avatar
求助一道sql问题,谢谢# JobHunting - 待字闺中
j*g
1
结果跟标准答案不一样。。。
http://sqlzoo.net/wiki/Self_join
表格基本信息:
stops(id, name)
route(num,company,pos, stop)
1) 大体上company+num合在一起算是一条线路的unique key。
2) route.stop跟stop.id相合
题目:
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.
给的提示:
Self-join twice to find buses that visit Craiglockhart and Sighthill, then
join those on matching stops.
我的sql code
SELECT a.num, a.company, stopb.name, d.num, d.company
FROM
route a
JOIN route b ON
(a.company=b.company AND a.num=b.num and a.stop<>b.stop)
JOIN route c ON
(b.stop=c.stop)
JOIN route d on
(c.company=d.company AND c.num=d.num and c.stop<>d.stop)
JOIN stops stopa ON (a.stop=stopa.id)
JOIN stops stopb ON (b.stop=stopb.id)
JOIN stops stopc ON (c.stop=stopc.id)
JOIN stops stopd ON (d.stop=stopd.id)
WHERE stopa.name='Craiglockhart' and stopd.name='Sighthill' and (a.num<>d.
num or a.company<>d.company);
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。