gpt4 book ai didi

mysql查询 - 2个外键

转载 作者:行者123 更新时间:2023-11-29 02:35:38 25 4
gpt4 key购买 nike

我有这两张表

airports: airportid, airportname
flights: flightid, from_airport, to_airport

其中 from_airportto_airport 是外键。

我可以在 airportidfrom_airportairportidto_airport 上加入表,或者我得到名称to_airportfrom_airport 的名称,但我想在一个查询中或至少同时选择 to_airportfrom_airport 名称成本。

这可能吗?怎么办??

这是我的查询:

SELECT
flight.idflight,
flight.idairline,
flight.from_airport,
flight.to_airport,
flight.number,
airports.name AS origin
FROM
flight
Inner Join airports ON flight.from_airport = airports.idairports

最佳答案

在进行连接时为您的表添加别名:

SELECT
flight.idflight,
flight.idairline,
flight.from_airport,
flight.to_airport,
flight.number,
airport_from.name AS origin
airport_to.name AS destination
FROM flight
INNER JOIN airports airport_from ON flight.from_airport = airport_from.idairports
INNER JOIN airports airport_to ON flight.to_airport = airport_to.idairports

关于mysql查询 - 2个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5504166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com