我想要一个查询来获取代码列中包含“cs”,“cp”,“is”的主题。我会用什么查询?
样本表
Subject | Code
1 | cs
1 | cp
1 | is
2 | cp
2 | cs
3 | cs
3 | cp
3 | is
4 | cp
所以输出必须是
subject
1
3
答案 0 :(得分:2)
您可以使用group by
和having
:
select subject
from t
where code in ('cp', 'cs', 'is')
group by subject
having count(distinct code) = 3;