![](/img/trans.png)
[英]MYSQL query for selecting the result of a condition of one table and showing it on another table
[英]mysql getting result from one table to query in another
我有兩個表,其中一個包含飛機場的列表以及到達飛機場的時間。 另一個包含從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');
我的問題是如何執行此操作,為什么我嘗試返回一個空集?
不確定我是否完全理解,但似乎您過於復雜了。
此查詢應返回給定航空公司的時間:
SELECT plane_dist.Time, plane_mapping.Airline
FROM plane_dist
JOIN plane_mapping
ON plane_dist.AirportID = plane_mapping.AirportID
WHERE Airline=('American Airlines');
您在這里不需要嵌套選擇,聯接實際上是合並兩個表的列,因此您可以在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'
請嘗試以下操作:
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
子句可用。
您通過子查詢獲得的原始條件過於復雜,可能會返回多個結果,但這是無效的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.