How to do an INNER JOIN on multiple columns

前端 未结 5 2067
春和景丽
春和景丽 2020-11-30 16:57

I\'m working on a homework project and I\'m supposed to perform a database query which finds flights either by the city name or the airport code, but the flights

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 17:37

    You can JOIN with the same table more than once by giving the joined tables an alias, as in the following example:

    SELECT 
        airline, flt_no, fairport, tairport, depart, arrive, fare
    FROM 
        flights
    INNER JOIN 
        airports from_port ON (from_port.code = flights.fairport)
    INNER JOIN
        airports to_port ON (to_port.code = flights.tairport)
    WHERE 
        from_port.code = '?' OR to_port.code = '?' OR airports.city='?'
    

    Note that the to_port and from_port are aliases for the first and second copies of the airports table.

提交回复
热议问题