2013-03-08 192 views
0

嗨,我正在爲下面的數據庫結構寫一個mysql查詢。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) 

誰教人數最多的課程在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請幫忙。

+0

你應該閱讀有關明確連接,GROUP BY和聚集。 – 2013-03-08 11:48:23

回答

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 
+0

這是哪個問題我有2 – user1844638 2013-03-08 11:45:02

+0

@ user1844638你不能至少測試一下,看看你自己嗎? – 2013-03-08 11:46:07

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