2

我有 3 张桌子:

表格1

RouteID             int
departure_stop      int 
destination_stop    int 
Price               decimal(6,2) 

表2

StopID          int
Stop            varchar(50)
Cities_CityID   int

表3

CityID      int
City        varchar(50)     

我想获得所有比 50 便宜的路线。我想像这样获得出发城市和目的地城市

City    City
Paris   London
Madrid  Zagred

到目前为止,我有两个查询我得到了出发城市,然后是第二个查询目的地城市,但不知道如何并排加入它们。这是我的查询:

SELECT
    Cities.City
FROM Routes, Stops, Cities
WHERE (Stops.StopID = Routes.departure_stop AND Stops.Cities_CityID = Cities.CityID AND Routes.price < '50') 

SELECT
    Cities.City
FROM Routes, Stops, Cities
WHERE (Stops.StopID = Routes.destination_stop AND Stops.Cities_CityID = Cities.CityID AND Routes.price < '50')

所以问题是如何将它们并排连接在一起,以便我可以看到出发地和目的地?

4

1 回答 1

0
Select
    DepartureCity.City AS 'Departure City',
    DestinationCity.City AS 'Destination City'
FROM Routes
INNER JOIN Stops DepartureStop    ON DepartureStop.StopID   = Routes.departure_stop
INNER JOIN Stops DestinationStop  ON DestinationStop.StopID = Routes.destination_stop 
INNER JOIN Cities DepartureCity   ON DepartureCity.CityID   = DepartureStop.Cities_CityID 
INNER JOIN Cities DestinationCity ON DepartureCity.CityID   = DestinationStop.Cities_CityID 

WHERE Routes.price < 50
于 2013-05-23T08:44:44.107 回答