简体   繁体   English

SQL选择和分组依据

[英]SQL- Select and Group By

I want to have a query that will get the subject that has "cs", "cp", "is" in code column. 我想查询一个将在代码列中包含“ cs”,“ cp”,“ is”的主题。 what query will i use ? 我将使用什么查询?

sample table 样品台

Subject | Code
1 | cs
1 | cp
1 | is
2 | cp
2 | cs
3 | cs
3 | cp
3 | is
4 | cp

so the output must be 所以输出必须是

subject
1
3

You can do this using group by and having : 您可以使用group byhaving

select subject
from t
where code in ('cp', 'cs', 'is')
group by subject
having count(distinct code) = 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM