如何编写这个复杂的SQL查询?

时间:2016-03-17 18:15:46

标签: mysql sql join

enter image description here

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薪水的飞行员,它会返回aname,如果所有飞行员的收入都超过60000,那么我需要找到这个名称。

3 个答案:

答案 0 :(得分:5)

你可以寻找相反的情况 - 没有飞行员的收入低于60,000:

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
    )

答案 1 :(得分: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)

答案 2 :(得分:-1)

选择     一个名字 从     飞机A. 哪里      EXISTS     (         选择 *         来自认证C         INNER JOIN员工E ON             E.eid = C.eid AND             E.salary> 60000         在哪里C.aid = A.aid     )