[英]How do I retrieve two values from the same column based on two different conditions in MySQL?
Table name: Route 表名:路线
Route_ID (int)
Route_From (int)
Route_To (int)
Table name: Stations 表名:站点
Station_ID (int),
Name (char)
Values of Route_From
, Route_To
, and Station_ID
are the same. Route_From
, Route_To
和Station_ID
的值相同。
I want to know how to retrieve two values from stations.name for two different conditions. 我想知道如何从两个不同条件的stations.name中检索两个值。 For example, something like
例如,像
select s.name, s.name from stations s, route r,
where route_from=1 and
route_to=3;
How do I do this? 我该怎么做呢?
Maybe you can do like this: 也许你可以这样做:
SELECT
Route.Route_ID,
fromStation.Name,
toStation.Name
FROM
Route
LEFT JOIN Stations AS fromStation
ON Route.Route_From =fromStation.Station_ID
LEFT JOIN Stations AS toStation
ON Route.Route_To =toStation.Station_ID
WHERE
Route.Route_ID = 1;
I think you want something more like this: 我想你想要更像这样的东西:
select s1.name as route_from, s2.name as route_to from route r
left join stations s1 on r.route_from = s1.Station_ID
left join stations s2 on r.route_to = s2.Station_ID
where Route_ID = 1;
SELECT
Route.Route_ID,
fromStation.Name,
toStation.Name
FROM
Route
LEFT JOIN Stations AS fromStation
ON Route.Route_From =fromStation.Station_ID
LEFT JOIN Stations AS toStation
ON Route.Route_To =toStation.Station_ID
WHERE
Route.Route_ID = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.