0

嗨,我正在为以下数据库结构编写一个 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)

返回在 Class 10 中教授课程数量最多的教授的姓名

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新手,请帮忙。

4

2 回答 2

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
于 2013-03-08T11:41:44.807 回答
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
于 2013-03-08T11:46:57.970 回答