我有以下表格
找到所有参加过sid = 1课程的sid。
select sid from enroll where cid in (select cid from enroll where sid=1)
group by sid having count(*)=(select count(*) from enroll where sid=1)
minus (select sid from student where sid=1);
答案 0 :(得分:0)
第一个COUNT(*)
是指属于特定sid
的行数。
重新格式化:
SELECT sid
FROM enroll
WHERE cid IN (SELECT cid FROM enroll WHERE sid=1)
GROUP by sid
HAVING COUNT(*) = (SELECT COUNT(*) FROM enroll WHERE sid=1)
MINUS (SELECT sid FROM student WHERE sid=1);
你当然可以通过使用:
来避免使用MINUSSELECT sid
FROM enroll
WHERE cid IN (SELECT cid FROM enroll WHERE sid=1)
AND sid != 1
GROUP by sid
HAVING COUNT(*) = (SELECT COUNT(*) FROM enroll WHERE sid=1)
答案 1 :(得分:0)
试试这个
SELECT s.sid
FROM student s, enroll e
WHERE s.sid = e.sid
AND e.cid IN (SELECT cid FROM course WHERE sid=1)
AND s.sid <> 1