我有4张桌子(型号,牌照,飞行员和人)
Model: mid, name
License: mid, pid, licenseDate
Pilot: pid, hireDate
person: pid, firstName, lastName
我想获得未获准驾驶飞机的飞行员的名字
我尝试了这个查询,但它没有显示结果!!!
select model.mid, license.pid, license.mid, license.licenseDate, pilot.pid, pilot.hireDate, person.firstName, person.lastName
from model, license, pilot, person
where pilot.pid = license.pid and model.mid = license.mid and pilot.pid = person.pid
and not exists(SELECT null FROM model WHERE pilot.pid = license.pid);
答案 0 :(得分:0)
尝试使用内连接。
SELECT model.mid, license.pid, license.mid, license.licenseDate, pilot.pid, pilot.hireDate, person.firstName, person.lastName
FROM model
INNER JOIN license ON (model.mid = license.mid)
INNER JOIN pilot ON (pilot.pid = license.pid)
INNER JOIN person ON (pilot.pid = person.pid)
WHERE pilot.pid = license.pid AND model.mid = license.mid AND pilot.pid = person.pid
AND pilot.pid != license.pid);
答案 1 :(得分:0)
未测试脚本,但您可以获取不在许可证表中的飞行员意味着他们没有获得驾驶飞机的许可,然后您可以将结果与人员表一起获取特定名称。
select p.firstname, p.lastname
from
(
select pid from pilot
where not exists (select pid from License where License.pid = pilot.pid)
) DT
inner join person p
on p.pid = DT.pid
答案 2 :(得分:0)
尝试一下:
SELECT pe.firstName, pe.lastName FROM pilot p
LEFT JOIN license l ON p.pid = l.pid
JOIN person pe ON p.pid = pe.pid
WHERE l.pid IS NULL