Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multidimensional "WHERE IN" condition?

Tags:

sql

mysql

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.

like image 572
Hagen von Eitzen Avatar asked Jul 05 '26 19:07

Hagen von Eitzen


2 Answers

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

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

Demo

like image 129
Tim Biegeleisen Avatar answered Jul 07 '26 10:07

Tim Biegeleisen


select *
from tbl
where (c_a, c_b) in (select c_a, c_b  from tbl_2);

here is a demo

like image 45
VBoka Avatar answered Jul 07 '26 09:07

VBoka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!