我有一张这样的表:
Table1
site | value
a | banana
b | banana
c | banana
a | fred
c | quetzal
a | quetzal
假设我有一组包含网站值的行
a
b
c
如何找到表1中的哪些值包含网站的a,b和c中的所有三个?
我知道
SELECT value
FROM Table1
WHERE site IN (SELECT site FROM chosen_sites)
将返回至少一个所选站点的所有值,但如何选择这三个中的值?
答案 0 :(得分:2)
在我的脑海中,我想是这样的:
select t.value
from Table1 t
join chosen_sites s on s.site = t.site
group by t.value
having count(distinct t.site) = (select count(*) from chosen_sites)
修改:seems to work。
答案 1 :(得分:1)
您可以cross join
使用网站和left join
table1的所有不同值,并检查计数。
select v.value
from table2 t2 --this is your table2 with all sites
cross join (select distinct value from table1) v
left join table1 t1 on t1.site=t2.site and t1.value=v.value
group by v.value
having count(t2.site)=count(t1.site)