考虑数据库建模课程的以下模式:
部门(做了,名字),
COURSE (cid, did, name, num, creditHours),
学生(sid、fname、lname、did)
ENROLLED_IN (eid, sid, cid)
列出参加每门 COMP 课程的所有学生的名字和姓氏 (DEPARTMENT.name="COMP")
考虑数据库建模课程的以下模式:
部门(做了,名字),
COURSE (cid, did, name, num, creditHours),
学生(sid、fname、lname、did)
ENROLLED_IN (eid, sid, cid)
列出参加每门 COMP 课程的所有学生的名字和姓氏 (DEPARTMENT.name="COMP")
使用带有 HAVING 子句的 GROUP BY 来断言注册的 COMP 课程的数量:
SELECT s.fname, s.lname
FROM STUDENT s
JOIN ENROLLED_IN e ON s.sid = e.sid
JOIN COURSE c ON e.cid = c.cid
JOIN DEPARTMENT d ON c.did = d.did
WHERE d.name = 'COMP'
GROUP BY 1, 2
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM COURSE c
JOIN DEPARTMENT d ON c.did = d.did
WHERE d.name = 'COMP')
试试这个加入
SELECT s.* FROM STUDENT s
JOIN ENROLLED_IN e ON (s.sid = e.sid)
JOIN DEPARTMENT d ON(s.did = d.did)
JOIN COURSE c ON(d.did = c.did)
WHERE d.name="COMP"