MySQL查询等同于"AND",而"IN"则等于是"OR"? [英] MySQL query equivalent of "AND", whereas "IN" is "OR"?
问题描述
我试图找到一个查询,该查询选择一个关联表的每一行,其中第二列表示必须与第一列的所有值都匹配的不同值.
I'm trying to find a query that selects every row of an associative table where the second column indicates different values that must all be matched with the first column's.
示例:我有X和Y列.我想获得X的值,其中X是在指定每个Y的情况下定义的.
Example: I have column X and Y. I want to get the values of X where X is defined with every 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)
该查询当然是无效的.我希望以某种方式得到a
和c
.
This query of course isn't valid. I would expect to get a
and c
somehow.
由于我也在尝试更好地学习MySQL查询,因此,如果您能提供一个答案,那么请您给出答案背后的逻辑解释,我们将不胜感激.谢谢! :)
As I'm also trying to learn MySQL queries better, I would appreciate if you could give an explanation of the logic behind your answer if you can provide one. Thanks! :)
推荐答案
我希望这是您想要的.如果您确认,将为您解释该查询.
I hope this is what you're looking for. If you confirm it,I'll explain you the query.
select x
from table
where y in (2,3)
group by x
having count(distinct(y)) = 2
这篇关于MySQL查询等同于"AND",而"IN"则等于是"OR"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!