我必须选择从'München'到'Düsseldorf'的所有航班并返回。表flightexecutions包含查找正确航班的信息。
我的陈述如下所示,我只能选择FlightNo和机场“来自”。
我不知道如何设置我的SELECT语句以获得包含FlightNo,from和to列的所需输出。
<pre id="pre"></pre>
--1)statements works correct, returns all FlightNo and origin "from"
SELECT flx.FlightNo, air.AirportName as `from`, air.AirportName as `to`
FROM flightexecution as flx, airport.airport as air
WHERE flx.ICAO_Code_Origin = air.ICAO_Code and (air.AirportName LIKE 'München (Franz Josef Strauß)'
OR air.AirportName LIKE 'Düsseldorf International');
--2)returns all flightNo and destination "to"
SELECT flx.FlightNo, air.AirportName as `to`
FROM flightexecution as flx, airport.airport as air
WHERE flx.ICAO_Code_Destination = air.ICAO_Code and (air.AirportName LIKE 'München (Franz Josef Strauß)'
OR air.AirportName LIKE 'Düsseldorf International');
output1):
FlightNo | from
-----------------
|
output2):
FlightNo | to
-----------------
|
desired output:
FlightNo | from | to
-------------------------
| |
答案 0 :(得分:1)
这个怎么样:
WHERE (flx.ICAO_Code_Origin = air.ICAO_Code
and (air.AirportName LIKE 'München (Franz Josef Strauß)'
OR air.AirportName LIKE 'Düsseldorf International'))
AND (flx.ICAO_Code_Destination = air.ICAO_Code
and (air.AirportName LIKE 'München (Franz Josef Strauß)'
OR air.AirportName LIKE 'Düsseldorf International'));
答案 1 :(得分:1)
应该这样做;它将所有flightexecutions独立地连接到它们的起始和目的地机场,然后使用WHERE条件将连接结果过滤到只有那些具有所需端点的那些。
SELECT fx.FlightNo, o.AirportName AS `from`, d.AirportName AS `to`
FROM flightexecution AS fx
INNER JOIN airport AS o ON fx.ICAO_Code_Origin = o.ICAO_Code
INNER JOIN airport AS d ON fx.ICAO_Code_Destination = d.ICAO_Code
WHERE (o.AirportName = 'München (Franz Josef Strauß)' AND d.AirportName = 'Düsseldorf International')
OR (d.AirportName = 'München (Franz Josef Strauß)' AND o.AirportName = 'Düsseldorf International')
旁注:由于MySQL处理OR
条件的方式(差);如果数据库很大,那么通过UNIONing这个查询的两个版本(一个包含WHERE条件的每个OR部分)可能会有更好的性能。
答案 2 :(得分:0)
SELECT flx.FlightNo AS flightname, air.AirportName as
从, airTo.AirportName as
到FROM flightexecution as flx, airport.airport as air,airport.airport as airTo, flightexecution as flxFrom WHERE (flx.ICAO_Code_Origin = air.ICAO_Code and (air.AirportName LIKE 'München (Franz Josef Strauß)'OR air.AirportName LIKE 'Düsseldorf International')) AND (flxFrom.ICAO_Code_Destination = airTo.ICAO_Code and (airTo.AirportName LIKE 'München (Franz Josef Strauß)' OR airTo.AirportName LIKE 'Düsseldorf International'));
试试这个