MySQL IN()有两个值/数组吗? [英] MySQL IN() for two value/array?
问题描述
我在寻找一种更好的方法来搜索MySQL以查找表中的一对值时遇到了麻烦.我将值对放在一个数组中,想复制IN()函数,但要使用多个值.
I'm having trouble finding a better way to search MySQL for a pair of values in a table. I have the value pairs in an array, and would like to duplicate the IN() function, but for more than 1 value.
例如用于目标;我有以下3对:
For example purposed; I have the following 3 pairs:
foo,1
boo,2
goo,3
当前的解决方案使我处于:
The current solution puts me at:
SELECT * FROM [table] WHERE
(column1 = 'foo' AND column2 = 1) OR
(column1 = 'boo' AND column2 = 2) OR
(column1 = 'goo' AND column2 = 3);
我想想有一种更性感"的解决方案,因为我可以拥有多达一百对,而OR会让我恶心.谢谢!!!
I'd like to think there's a more "sexy" solution seeing that I could have as many as a hundred pairs and having that may ORs kind of makes me nauseous. Thanks!!!
推荐答案
SELECT *
FROM foo
WHERE (column1, column2) IN (('foo', 1), ('bar', 2))
此语法可能会造成混淆,并且用以下内容替换可能更容易理解:
This syntax may be confusing, and it may be more readable to replace it with:
SELECT *
FROM foo
WHERE ROW(column1, column2) IN (ROW('foo', 1), ROW('bar', 2))
我已经习惯了前一个,:)
I'm used to the former one, though :)
这篇关于MySQL IN()有两个值/数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!