MySQL query is equivalent to "AND", while "IN" is "OR"?

I am trying to find a query that selects each row of an associative table, where the second column indicates different values ​​that should be mapped to the first column.

Example: I have a column X and Y. I want to get the values ​​of X, where X is defined with each Y specified.

x    y
======
a    1
a    2
b    1
a    3
c    2
c    3

SELECT DISTINCT x FROM table WHERE y AND (2, 3)

This request is of course invalid. I would expect to get somehow aand c.

Since I am also trying to study MySQL queries better, I would appreciate if you could give an explanation of the logic of your answer, if you can provide one. Thank you :)

+5
source share
1 answer

, , . , .

select x
from table
where y in (2,3)
group by x
having count(distinct(y)) = 2
+11

All Articles