我正在研究样本铁路数据库。我被困在检索一些数据。表结构是
培训:
TrainId | TrainName | FromStationId | ToStationId | DeptTime | ArrivalTime
...
...
RouteDetail:
Id | TrainId | RouteOrder | FromStationId | ToStationId | TrackId
...
...
电台:
Id | name
...
...
现在我正在尝试检索这些数据,但我还没有成功。
TrainName | RouteOrder | FromStationName | ToStationName
非常感谢任何帮助
感谢。
答案 0 :(得分:4)
像这样的东西。如果不是,您必须提供更多详细信息。
SELECT
T.TrainName
, RD.RoutOrder
, S1.name as FromStationName
, S2.name as ToStationName
FROM Train T
LEFT JOIN RouteDetail RD
ON T.TrainId = RD.TrainId
LEFT JOIN Station S1
ON RD.FromStationId = S1.Id
LEFT JOIN Station S2
ON RD.ToStationId = S2.Id
答案 1 :(得分:2)
select t.trainname, r.routeorder, sfrom.name as fromstationname, sto.name as tostationname
from train t
inner join routedetail r on r.trainid = t.trainid
left join station sfrom on sfrom.id = fromstationid
left join station sto on sto.id = tostationid