表:
这是我的数据库模式,我需要从该模式获取:
归还所有教授授课的课程。
我写了以下查询,这显然是错误的,任何人都可以帮助我,因为我是SQL的新手。
SELECT
c.coursename
FROM
professor p, works w, course c
WHERE
p.empid = w.empid
AND w.courseid = c.courseid
AND p.empid IN (SELECT all(empid) FROM professors)
答案 0 :(得分:3)
此问题称为Relational Division
。从您想要的问题:
“ ..归还所有教授所教授的课程.. ”
你的头衔与你想要的完全相反。
此查询将为您提供,
SELECT a.CourseID, a.Course
FROM Course a
INNER JOIN Works b
ON a.CourseID = b.CourseID
INNER JOIN Professor c
ON b.EmpID = c.EmpID
GROUP BY a.CourseID, a.Course
HAVING COUNT(DISTINCT c.EmpID) = (SELECT COUNT(*) FROM Professor)
答案 1 :(得分:0)
尝试此查询
SELECT
courseName
FROM
Course c
WHERE
NOT EXISTS (SELECT
EMP_ID
FROM
Professor p,
works w
WHERE
c.CourseID = w.CourseID AND
p.EMP_ID <> w.EMP_ID)