您好我正在为以下数据库结构编写一个mysql查询。
Professor (EMP ID,Name,Status,Salary,Age)
Course(Course ID,Course Name,Points)
Works(Course ID,EMP ID,Class ID)
Assumptions:
Each course has only one instructor in each semester
Status can take values from “Full”, “Associate”, and “Assistant”
我需要做以下事情。
返回曾教过至少两门课程的全职教授的姓名
SELECT p.name
FROM professor p, works w
WHERE p.empid = w.empid
AND p.status = ‘full’
AND w.classid IN (SELECT classid
FROM works
WHERE count(courseid)>1)
返回教授课程数量最多的教授的姓名
SELECT p.names
FROM professor p, works w
WHERE p.empid =w.empid
AND w.classid IN (SELECT classid
FROM works
WHERE classid = 10
AND coursed = max(coursed))
但是这些查询返回了错误的结果。我是mysql的新手请帮忙。
答案 0 :(得分:1)
尝试这样
SELECT
p.*,
COUNT(c.Course_ID) AS TCount
FROM Professor AS P
LEFT JOIN Works AS w ON w.EMP_ID = p.EMP_ID
LEFT JOIN Course AS c ON w.Course_ID = c.Course_ID
GROUP BY p.EMP_ID , c.Course_ID
HAVING TCount > 1
答案 1 :(得分:1)
第一个的替代答案:
SELECT DISTINCT p.empid, p.name
FROM professor AS p
JOIN works AS w ON
p.empid = w.empid
JOIN works w2 ON
w.empid=w2.empid AND w.classid=w2.classid AND w.courseid<>w2.courseid