我有两张表,其中包含机场列表和到达机场的时间。另一个包含从planeID到航空公司的映射。
表:plane_dist
+----------+-----------+--------+
| PlaneID | AirportID | Time |
+----------+-----------+--------+
| P234567 | A12312 | 1.2 |
| P234567 | A12315 | 1.1 |
+----------+-----------+--------+
表:plane_mapping
+----------+--------------------+-----------+--------------------+
| PlaneID | Airline | AirportID | Location |
+----------+--------------------+-----------+--------------------+
| P234567 | American Airlines | A12312 | Los Angeles |
| P234566 | Delta Airlines | A12315 | San Diego |
+----------+--------------------+-----------+--------------------+
鉴于航空公司,我需要返回它可以去的机场的位置,时间。
我试过了,但总是返回一个空集:
SELECT plane_dist.Time, plane_mapping.Airline
FROM plane_dist JOIN plane_mapping
on plane_dist.AirportID = plane_mapping.AirportID
WHERE
plane_dist.PlaneID=
(SELECT DISTINCT PlaneID FROM plane_mapping WHERE Airline=('American Airlines'));
我分别尝试了两个查询并返回了预期的结果:
SELECT plane_dist.Time, plane_mapping.Airline
FROM plane_dist JOIN plane_mapping
on plane_dist.AirportID = plane_mapping.AirportID
WHERE
plane_dist.PlaneID=('P234567');
SELECT DISTINCT PlaneID
FROM plane_mapping WHERE Airline=('American Airlines');
我的问题是我该怎么做以及为什么我的尝试会返回一个空集?
答案 0 :(得分:0)
我不确定我是否完全理解,但似乎你过度复杂了。
此查询应返回给定航空公司的时间:
SELECT plane_dist.Time, plane_mapping.Airline
FROM plane_dist
JOIN plane_mapping
ON plane_dist.AirportID = plane_mapping.AirportID
WHERE Airline=('American Airlines');
答案 1 :(得分:0)
此处不需要嵌套选择,连接实际上是组合两个表的列,因此您只需在where子句中过滤航空公司:
SELECT plane_dist.Time, plane_mapping.Airline
FROM plane_dist JOIN plane_mapping
on plane_dist.AirportID = plane_mapping.AirportID
WHERE
plane_mapping.Airline = 'American Airlines'
答案 2 :(得分:0)
尝试以下方法:
SELECT plane_dist.Time, plane_mapping.Airline
FROM plane_dist JOIN plane_mapping
on plane_dist.AirportID = plane_mapping.AirportID
WHERE Airline='American Airlines'
通过您的JOIN
子句,plane_mapping表的所有列都可用于您的WHERE
子句。
通过子查询的原始条件太复杂,可能会返回多个结果,但无效。