开发者

MYSQL IN on 2 columns?

开发者 https://www.devze.com 2023-01-31 21:50 出处:网络
Is it possible to do smthng like SELECT 开发者_StackOverflow中文版* FROM table WHERE x1,x2 IN (values)?If you need matching in pairs:

Is it possible to do smthng like SELECT 开发者_StackOverflow中文版* FROM table WHERE x1,x2 IN (values)?


If you need matching in pairs:

SELECT  *
FROM    table
WHERE   (x1, x2) IN ((1, 2), (3, 4))

will match only 1, 2 or 3, 4

If you need cross-matches:

SELECT  *
FROM    table
WHERE   x1 IN (1, 3)
        AND x2 IN (2, 4)

will match any of the 1, 2, 3, 2, 1, 4 or 3, 4


I don't believe that's possible - you simply have to either state...

SELECT * FROM table WHERE (x1 IN (values) OR x2 IN (values)) ..

or

SELECT * FROM table WHERE (x1 IN (values) AND x2 IN (values)) ..

...depending on the logic you require.


It should be like this

SELECT * FROM table WHERE (x1,x2) IN ((value1,value2), (value3,value4));

You can even do something like this:

SELECT * FROM table WHERE (x1,x2) IN (SELECT x3,x4 FROM another_table);

Of course, x3, x4 have to match the data type of x1, x2 respectively.


Depending on how you mean it, you'd have to write

... WHERE x1 IN (values) AND x2 IN (values)
... WHERE x1 IN (values) OR  x2 IN (values)

But you'll have to duplicate the values list

0

精彩评论

暂无评论...
验证码 换一张
取 消