MYSQL checking all values in a set match another set

↘锁芯ラ 提交于 2019-12-25 02:44:50

问题


I have two tables A and B with values below

Table A

   X
------
1
2
3

Table B

X       Y
------  ------
1       A
2       A
3       A
1       B
2       B
1       C
3       D

I need to find only the Y values from Table B which match All of the values in Table A. So for the above example the only Y value that matches is A (A has an X value of 1,2,and 3)


回答1:


This is an example of a "set-within-sets" subquery. I like to approach this with aggregation and a having clause.

select b.y
from tableB b join
     tableA a
     on b.X = a.X
group by b.y
having count(distinct b.x) = (select count(*) from tableA);


来源:https://stackoverflow.com/questions/22394607/mysql-checking-all-values-in-a-set-match-another-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!