2014-01-19 129 views
0

我有一个小问题。我正在学习数据库考试,因此我无法真正测试问题中提出的查询。假设我们有这些表:SQL查询问题

PlaneModel = {id, number_seats} 
Pilot = {id, name, telephone, birth_date} 
Plane = {id, name, model} 
Flight = {id, date, id_Plane, id_Pilot, duration, origin, destiny} 

的运动是:建立一个查询,将显示由现有的所有飞行员驾驶飞机。

我来什么事到是这样的:

select p.id from Plane p, Pilot pi, Flight f 
where f.id = pi.id_Pilot and f.id_Plane = p.id 
group by p.id 
having f.id_Pilot in all (select id from Pilot); 

但我不很确定,如果这个工程,因为我无法测试它。这样对吗?如果不是,你建议什么?

+2

我建议您使用适当的'join'语法。如果你的导师没有教导,那么采取更好的方法。 –

+0

在计算机上安装RDBMS以测试您的想法与 – suspectus

+2

真的很有用您可以在www.sqlfiddle.com上测试。 –

回答

2

你的查询是不正确的,因为having f.id_Pilot in all (select id from Pilot)不是正确的方式问“所有飞行员驾驶”的问题。

相反,你可以要求每个飞机驾驶的不同驾驶员的数量与所有已知驾驶员的数量相同。另外,您应该将查询的语法切换为ANSI SQL连接语法:

SELECT p.id 
FROM Plane p 
JOIN Flight f ON f.id_Plane = p.id 
JOIN Pilot pi ON f.id = pi.id_Pilot 
GROUP BY p.id 
HAVING COUNT(DISTINCT pi.id_Pilot) = (SELECT COUNT(*) FROM Pilot) 
+0

没想到那样。感谢你的回答! –