SQL 不同条件多 select

SQL different condition multi select

我想做的(对于每个航班),是 select Flight_number、Departure_airport 的 Name 和 Arrival_airport 的 姓名。出发有 MIN Leg_number,到达有 MAX Leg_number.

这个我试过了。但是加入部分或其他缺失的东西,这里是 link:http://sqlfiddle.com/#!2/263a2/5

不完全确定这是否是您想要的。它是用 MS SQL 编写的,因此语法需要一些小的调整。

SELECT fl.Flight_number,
ao.Name,
ai.Name,
(select min(Leg_number) from FLIGHT_LEG fa where fl.Flight_number
    = fa.Flight_number) as min_leg_number,

(select max(Leg_number) from FLIGHT_LEG fb where fl.Flight_number
    = fb.Flight_number) as max_leg_number
FROM Flight_leg Fl
inner join AIRPORT as ao on fl.Departure_airport_code =
    ao.Airport_code
inner join AIRPORT as ai on fl.Arrival_airport_code =
    ai.Airport_code

看起来很奇怪..但这可能就是你想要的...... 我们在别名为 "Z" 的子查询中得到每个航班的 min/max 航段 我们使用它两次返回 flight_leg,一次用于出发,一次用于到达 然后再次返回机场两次,一次是出发,一次是到达。

 SELECT Z.Flight_Number, DA.Name DeptName, AA.Name ArrivName
    FROM (SELECT MIN(Leg_Number) MLN, MAX(Leg_Number) MxLN, Flight_Number
               FROM Flight_Leg Group by Flight_Number) Z
    INNER JOIN Flight_Leg D
     on D.Flight_Number = Z.Flight_Number 
    and D.Leg_Number = Z.MLN
    INNER JOIN Flight_Leg A
     on A.Flight_Number = Z.Flight_Number
     and A.Leg_Number = Z.MxLN
    INNER JOIN AirPort DA
     on DA.AirPort_Code = D.Departure_AirPort_Code
    INNER JOIN AirPort AA
     on AA.AirPort_Code = A.Arrival_AirPort_Code

http://sqlfiddle.com/#!2/263a2/56