2016-03-17 181 views
1

enter image description here如何編寫這個複雜的SQL查詢?

enter image description here

我需要找到飛機的名字,使得所有飛行員認證來操作它們收入超過60000

查詢我寫道:

select aname 
from employee join certified 
on employee.eid=certified.eid 
join aircraft 
on certified.aid=aircraft.aid 
where salary>60000; 

但是,如果有超過60000美元薪水的飛行員返航,難度在於我需要找到所有飛行員賺取超過60000才顯示aname。

+0

where salary> 60000; – Pudding

+0

你可以使用'WHERE .. IN ALL(subquery)'。檢查文檔:https://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html – hjpotter92

+0

對不起,糾正它@Pudding – mightyWOZ

回答

5

你可以隨便找相反的情況 - 即沒有飛行員收入低於6:

SELECT 
    aname 
FROM 
    Aircraft A 
WHERE 
    NOT EXISTS 
    (
     SELECT * 
     FROM Certified C 
     INNER JOIN Employee E ON 
      E.eid = C.eid AND 
      E.salary < 60000 
     WHERE C.aid = A.aid 
    ) 
+0

擊敗我。我會upvote並刪除我的答案。 – Mureinik

+0

我的打字速度更快了:) –

+0

非常感謝,它真的很有幫助。它證明,還有很多谷歌無法告訴你的東西,但人們可以。再次感謝您@TomH – mightyWOZ

0
SELECT aname FROM Aircraft where NOT EXISTS (SELECT eid FROM Employee AS e INNER JOIN Certified AS c ON c.eid=e.eid WHERE salary<60000 AND aid=Aircraft.aid) 
-1

選擇 aname FROM 飛機 WHERE EXISTS ( SELECT * FROM Certified C INNER JOIN Employee E ON E.eid = C.eid AN D E.salary> 60000 WHERE C.aid = A.aid )

+0

在@Tom H的查詢中,把所有沒有員工的飛機名稱 – Vishal