IDEstudante IDCadeira IDProfessor Notas
100100 10100 100 12
100001 10100 103 11
100001 10103 100 14
100100 10103 103 10
100102 20100 201 15
我想搜索IDCadeira上同时具有值10100和10103的IDProfessor。我尝试了几种解决方案,但结果总是空洞的。这似乎很容易,这让我发疯了。
谢谢
答案 0 :(得分:1)
如果您只想知道至少有两行的IDProfessors,请将GROUP BY
与HAVING
合并:
select IDProfessor
from tablename
group by IDProfessor
having count(*) >= 2
答案 1 :(得分:0)
选择您感兴趣的记录(where idcadeira in (10100, 10103)
),按教授汇总(group by idprofessor
)并计算您是否拥有所有idcadeiras(在您的情况下为两个):
select idprofessor
from mytable
where idcadeira in (10100, 10103)
group by idprofessor
having count(distinct idcadeira) = 2;