我有2张桌子
EXPERTISE
EID,SCODE
101,4001
101,4002
101,4003
101,4004
101,4005
101,4006
102,4002
102,4003
102,4006
103,4001
103,4006
103,4004
104,4004
104,4006
105,4002
105,4003
105,4004
105,4006
106,4001
106,4002
106,4003
106,4004
106,4005
106,4006
和
特色
SCODE,SNAME
4001,Cardiology
4002,Pediatrics
4003,Oncology
4004,Psychiatry
4005,Opthamology
4006,Internal
我该如何制定一个查询来返回专门研究每个专业的EID?
答案 0 :(得分:2)
我认为最简单的方法是聚合和计数:
select e.eid
from expertise e
group by e.eid
having count(*) = (select count(*) from specialties);
这假设表没有重复,根据您的样本数据,这似乎是合理的。如果有重复项,则可以仅使用count(distinct)
代替count(*)
。
答案 1 :(得分:0)
SELECT EID
FROM EXPERTISE
GROUP BY EID
HAVING COUNT(SCODE) = (SELECT COUNT(*) FROM SPECIALTY)