使用别名时查询结果显示两次?

时间:2016-10-01 09:39:16

标签: mysql join duplicates

我有一个查询,我在MySQL中运行,它使用别名从同一列中获取信息两次(往返于airport表的目的地)。

`SELECT AirlineName AS 'Airline Name',
     airport1.AirportName AS 'Flying From',
     airport2.AirportName AS 'Flying To',
     StopType AS 'Number of Stops'
FROM flightjunction
INNER JOIN airline ON flightjunction.FlightAirline = airline.AirlineID
INNER JOIN airport AS airport1 ON flightjunction.FlightFrom = airport1.AirportID
INNER JOIN airport AS airport2 ON flightjunction.FlightTo = airport2.AirportID
INNER JOIN stops ON flightjunction.FlightStops = stops.StopID;`

但是,我发现在运行此查询时会出现重复的结果。我哪里错了?提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

由于在这种情况下你在同一个表上有两次内连接,你应该添加一个DISTINCT以消除重复。