剛開始上班,閒着沒什麼事情,搞到一份2012年1月1號的列車時刻表數據庫,是access版本的,稍後我會給出下載鏈接,研究了一下表結構,覺得這樣的表結構還是比較合理的。於是也考慮了一下大家經常使用的時刻表查詢工具的後臺SQL實現。我估計大多數的工具比如極品時刻表、路路通時刻表等等,都是這樣實現的。首先給出表結構,
基於這樣的表結構,給出常用的查詢SQL。
--車次查詢
select * from train where id='K7726/K7727';
--車站查詢
select * from train where station='龍家營';
--站站查詢
SELECT T1.id, T1.station, t1.d_time, T2.station, T2.a_time,t2.p1-t1.p1 as 票價
FROM train AS T1, train AS T2
WHERE T1.station='龍家營' and T2.Station='邯鄲' and T1.S_NO<T2.S_NO and T1.id=T2.id;
--中轉查詢(邯鄲到聊城沒有直達車)
select t1.id,t1.station,t2.station,t2.a_time,t3.id,t3.d_time,t4.station,(t2.distance-t1.distance)+(t4.distance-t3.distance) as 總里程
from train T1,train t2,train t3,train t4
where T1.station='邯鄲'
and t4.station='聊城'
and t1.id=t2.id and t1.s_no<t2.S_No
and t3.id=t4.id and t3.s_no<t4.s_no
and t2.station=t3.station and t3.d_time>t2.A_time
order by (t2.distance-t1.distance)+(t4.distance-t3.distance) asc;
下面給出數據庫的下載鏈接: