不知不覺(jué)還有兩個(gè)月又要十一小長(zhǎng)假了,對(duì)于我和閨蜜們,這是十分難得可以一起出游的機(jī)會(huì)。每年為了這個(gè)短途的國(guó)內(nèi)游,我們都要頭疼上一陣子,最主要的原因是我們?cè)诓煌某鞘泄ぷ鳎枰乙粋€(gè)大家都能到,也比較方便,又都沒(méi)去過(guò)的地方。
正好最近在學(xué)sql,于是我自告奮勇,想先從12306下手,看看能不能幫我做做篩選。說(shuō)干就干,我找到了一個(gè)開(kāi)源的爬蟲(chóng),可以拿到12306的車(chē)次信息。所以第一件事,我爬了所有從青島、北京、上海三個(gè)地方出發(fā)的車(chē)次信息,一共16493條。


下一步需要篩選出來(lái),三個(gè)地方都能夠到達(dá)的地方,發(fā)現(xiàn)我們這三個(gè)城市都能坐火車(chē)到達(dá)的有64個(gè)車(chē)站。
select distinct c.to_station
from
(
select a.from_station,a.arrive_time,b.from_station as from_station2,b.arrive_time as arrive_time2,a.to_station
from tb_train_5 a
join tb_train_5 b on a.to_station = b.to_station
and a.from_station like '%上海%'
and b.from_station like '%青島%') c
join tb_train_5 on tb_train_5.to_station = c.to_station
and tb_train_5.from_station like '%北京%'

64個(gè)車(chē)站還是無(wú)從下手,于是我又把我們?nèi)齻€(gè)城市分別的到達(dá)時(shí)間列出來(lái),發(fā)現(xiàn),雖然這些車(chē)站我們都能到達(dá),但是有一些車(chē)站三個(gè)人的到達(dá)時(shí)間差太大甚至到了一整個(gè)白天,本來(lái)時(shí)間就很緊張經(jīng)不起這么大段的浪費(fèi)。
select distinct c.from_station,c.arrive_time,c.from_station2,c.arrive_time2,tb_train_5.from_station as from_station3,tb_train_5.arrive_time,c.to_station
from
(
select a.from_station,a.arrive_time,b.from_station as from_station2,b.arrive_time as arrive_time2,a.to_station
from tb_train_5 a
join tb_train_5 b on a.to_station = b.to_station
and a.from_station like '%上海%'
and b.from_station like '%青島%') c
join tb_train_5 on tb_train_5.to_station = c.to_station
and tb_train_5.from_station like '%北京%'

跟閨蜜商量了一下,一致決定,我們希望能夠在2個(gè)小時(shí)只能都到齊,然后一起打車(chē)出發(fā)去賓館。于是我又對(duì)到達(dá)時(shí)間做了限制,得到了39個(gè)車(chē)站。
select distinct c.to_station
from
(
select a.from_station,a.arrive_time,b.from_station as from_station2,b.arrive_time as arrive_time2,a.to_station
from tb_train_5 a
join tb_train_5 b on a.to_station = b.to_station
and a.from_station like '%上海%'
and b.from_station like '%青島%'
and timestampdiff(minute,a.arrive_time,b.arrive_time)<=120
and timestampdiff(minute,b.arrive_time,a.arrive_time)<=120) c
join tb_train_5 on tb_train_5.to_station = c.to_station
and tb_train_5.from_station like '%北京%'
and timestampdiff(minute,tb_train_5.arrive_time,c.arrive_time)<=120
and timestampdiff(minute,c.arrive_time,tb_train_5.arrive_time)<=120
and timestampdiff(minute,c.arrive_time2,tb_train_5.arrive_time)<=120
and timestampdiff(minute,tb_train_5.arrive_time,c.arrive_time2)<=120

好啦,我要拿著這39個(gè)車(chē)站去找閨蜜啦。也只是最近在學(xué)sql就想著用一個(gè)真實(shí)案例來(lái)模擬一下,這個(gè)過(guò)程應(yīng)該有很多種sql的寫(xiě)法,如果小伙伴們有更好的方法歡迎與我交流啦。