【发布时间】:2015-12-29 02:43:25
【问题描述】:
我是 sql 初学者,尝试按照提示使用 self join 两次但失败了。
查找涉及两辆可以从 Craiglockhart 到 Sighthill 的巴士的路线。 显示巴士号码。第一班车的公司,换乘站点的名称, 和巴士号码。和公司的第二辆公共汽车。 提示:自行加入两次以查找访问 Craiglockhart 和 Sighthill 的巴士,然后加入匹配站点的巴士。
我的代码:
select
a.num, a.company, stopsc.name, c.num, c.company
from
route a
join route b
on (a.num=b.num and a.company=b.company) **From Craiglockhart to the transfer stop**
join route c
on (b.stop=c.stop) **to connect the transfer stop**
join route d
on (c.num=d.num and c.company=d.company) **From transfer stop to the final stop which is Sighthill**
join stops stopsa
on (a.stop=stopsa.id)
join stops stopsb
on (b.stop=stopsb.id)
join stops stopsc
on (c.stop=stopsc.id)
join stops stopsd
on (d.stop=stopsd.id)
where
stopsa.name='Craiglockhart'
and stopsd.name='Sighthill'
and a.num!=c.num **to delete the same route**
order by a.num
请告诉我我的回答有什么逻辑错误。我知道sqlselfjoin 上还有另一个答案,但我想知道我在哪一步出错了。非常感谢!
【问题讨论】: