gpt4 book ai didi

mysql - 加入两个mysql查询

转载 作者:行者123 更新时间:2023-11-29 03:39:15 24 4
gpt4 key购买 nike

我有 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')

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

最佳答案

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

关于mysql - 加入两个mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16709179/

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