0

表名:路线

Route_ID (int)
Route_From (int)
Route_To (int)

表名:站

Station_ID (int),
Name (char)

Route_FromRoute_To和的值Station_ID相同。

我想知道如何从stations.name 中检索两个不同条件的两个值。例如,像

 select s.name, s.name from stations s, route r, 
 where route_from=1 and
 route_to=3;

我该怎么做呢?

4

3 回答 3

2

也许你可以这样做:

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;
于 2012-04-27T11:22:36.487 回答
0
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;
于 2014-04-04T10:59:30.287 回答
0

我想你想要更像这样的东西:

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;
于 2012-04-27T11:25:03.987 回答