Skip to content
Advertisement

multidimensional “WHERE IN” condition?

As is well-known, we can simplify

SELECT * FROM A WHERE X=1 OR X=4 OR X=9

as follows:

SELECT * FROM A WHERE X IN (1,4,9)

Is there a similar shortcut possible for “multidimensional” conditions as in the following:

SELECT * FROM A WHERE (X=1 AND Y=2) OR (X=4 AND Y=3) OR (X=9 AND Y=5)

in a form resembling something like this

SELECT * FROM A WHERE [X,Y] IN ( [1,2], [4,3], [9,5] )

?

I would be willing to count anything as a simplification that uses O(n) characters where the factor is significantly smaller than in the naive example (i.e., adding one more case makes the query string grow by less than OR (X=... AND Y=...)) even though there is perhaps more constant overhead.

Advertisement

Answer

Yes, you may use WHERE IN (...) with tuples in MySQL:

SELECT *
FROM A
WHERE (x, y) IN ((1, 2), (4, 3), (9, 5));

Demo

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement