例如,如果我有两列火车,X和Y,他们旅行:
TRAIN STATIONS ………
> Train-X:经过Station-A;站-B;站-C;站-d
> Train-Y:经过Station-B;站-X;站-d;站-Y
我如何将这些信息放入数据库中,以便乘客询问哪些列车从Station-B开始?什么火车以车站D结束?那么Train-X和Train-Y都会出现在结果中.
最佳答案 我会说你需要有三个表来完成这项工作.
站:车站ID,名称等
服务:服务ID,操作符,列车车辆数量等.
Service_Stop:服务ID,停止号码,站点ID.
然后,您可以使用类似以下内容的查询找到在Station-B和随后在Station-D处停止的服务:
SELECT
Service_ID
FROM
Station AS Start_Station
JOIN Service_Stop AS Start_Stop ON
Start_Station.Station_ID = Start_Stop.Station_ID
JOIN Service_Stop AS End_Stop ON
Start_Stop.Service_ID = End_Stop.Service_ID AND
Start_Stop.Stop_Number < End_Stop.Stop_Number
JOIN Station AS End_Station ON
End_Stop.Station_ID = End_Station.Station_ID AND
End_Station.Name = "Station-D"
WHERE
Start_Station.Name = 'Station-B'