SQL 无法获取准确数据
SQL can't get the accurate data
我在 SQL 中获取数据时遇到问题。如何拥有一个 table 源或主键但有两个不同的外键。
This is the ERD
This is the result but when i put the Departure it will be vice versa
如果我没有正确理解您的问题,那么您似乎是在尝试从同一个连接中获取 "Departure" 和 "Arrival" 值。为了使其正常工作,您需要为 "Departure" 和 "Arrival" 中的每一个分别创建两个从 Routes 到 Airports 的单独连接(并相应地使用别名)。
select date,
time,
ArrivalAirport.IATACode as Departure,
DepartureAirport.IATACode as Arrival,
FlightNumber,
Aircrafts.Name,
EconomyPrice,
Schedules.ID
from Schedules
inner join [Routes] on Schedules.RouteID = [Routes].ID
inner join Aircrafts on Schedules.AircraftID = Aircrafts.ID
inner join Airports as ArrivalAirport on ArrivalAirport.ID = [Routes].ArrivalAirportID
inner join Airports as DepartureAirport on DepartureAirport.ID = [Routes].DepartureAirportID
请注意,"Airports" 连接的两个实例需要相应地为它们设置别名,以便您可以在 select 投影中执行所需的操作。
您需要为使用两次的 table 使用 table 别名,然后加入它两次。
SELECT Routes.ID, DEP.IATACode, ARR.IATACode
FROM dbo.Routes
INNER JOIN dbo.Airports AS DEP ON DEP.ID = Routes.DepartureAirportID
INNER JOIN dbo.Airports AS ARR ON ARR.ID = Routes.ArrivalAirportID
一旦定义,您就可以在查询中的任何地方使用别名。参见 here for more on using an alias
以下是我认为您正在寻找的内容:
SELECT
Schedules.Date,
Schedules.Time,
DepartureAirport.IATACode AS Departure,
ArrivalAirport.IATACode AS Arrival,
Schedules.FlightNumber,
Aircrafts.Name,
Schedules.EconomyPrice,
Schedules.ID
FROM
Schedules
INNER JOIN Aircrafts ON Schedules.AircraftID = Aircrafts.ID
INNER JOIN Routes ON Schedules.RouteID = Routes.ID
INNER JOIN Airports AS DepartureAirport
ON Routes.DepartureAirportID = DepartureAirport.ID
INNER JOIN Airports AS ArrivalAirport
ON Routes.ArrivalAirportID = DepartureAirport.ID
问题是有两个单独的连接到机场 table 这样你就可以有两个不同的机场。否则你的航班会很短,乘客会很不满意。
我在 SQL 中获取数据时遇到问题。如何拥有一个 table 源或主键但有两个不同的外键。
This is the ERD This is the result but when i put the Departure it will be vice versa
如果我没有正确理解您的问题,那么您似乎是在尝试从同一个连接中获取 "Departure" 和 "Arrival" 值。为了使其正常工作,您需要为 "Departure" 和 "Arrival" 中的每一个分别创建两个从 Routes 到 Airports 的单独连接(并相应地使用别名)。
select date,
time,
ArrivalAirport.IATACode as Departure,
DepartureAirport.IATACode as Arrival,
FlightNumber,
Aircrafts.Name,
EconomyPrice,
Schedules.ID
from Schedules
inner join [Routes] on Schedules.RouteID = [Routes].ID
inner join Aircrafts on Schedules.AircraftID = Aircrafts.ID
inner join Airports as ArrivalAirport on ArrivalAirport.ID = [Routes].ArrivalAirportID
inner join Airports as DepartureAirport on DepartureAirport.ID = [Routes].DepartureAirportID
请注意,"Airports" 连接的两个实例需要相应地为它们设置别名,以便您可以在 select 投影中执行所需的操作。
您需要为使用两次的 table 使用 table 别名,然后加入它两次。
SELECT Routes.ID, DEP.IATACode, ARR.IATACode
FROM dbo.Routes
INNER JOIN dbo.Airports AS DEP ON DEP.ID = Routes.DepartureAirportID
INNER JOIN dbo.Airports AS ARR ON ARR.ID = Routes.ArrivalAirportID
一旦定义,您就可以在查询中的任何地方使用别名。参见 here for more on using an alias
以下是我认为您正在寻找的内容:
SELECT
Schedules.Date,
Schedules.Time,
DepartureAirport.IATACode AS Departure,
ArrivalAirport.IATACode AS Arrival,
Schedules.FlightNumber,
Aircrafts.Name,
Schedules.EconomyPrice,
Schedules.ID
FROM
Schedules
INNER JOIN Aircrafts ON Schedules.AircraftID = Aircrafts.ID
INNER JOIN Routes ON Schedules.RouteID = Routes.ID
INNER JOIN Airports AS DepartureAirport
ON Routes.DepartureAirportID = DepartureAirport.ID
INNER JOIN Airports AS ArrivalAirport
ON Routes.ArrivalAirportID = DepartureAirport.ID
问题是有两个单独的连接到机场 table 这样你就可以有两个不同的机场。否则你的航班会很短,乘客会很不满意。