该题目是sqlzoo上self join最后一题。该数据集有2张表,表1、stops是公交站点名字,通过stops.id和route.stop关联。
表2、route:num代表公交车号码,company代表运营公司,pos代表公交车经停站点序号,stop是公交站点号码。
需求如下:
Find the routes involving two buses that can go from Craiglockhart to Sighthill.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus。
需求结果如下:
我自己的sql代码如下:
select distinct a.num,a.company,d.num,d.company
from route a left join route b on (a.company=b.company and a.num=b.num)
join route c on (b.stop=c.stop)
join route d on (c.company=d.company and c.num=d.num)
join stops stopa on (a.stop=stopa.id and stopa.name='Craiglockhart')
join stops stopb on (d.stop=stopb.id and stopb.name='Sighthill')
但我显示不出来中间换乘站点名字,求高手指教!感谢