目录
1.How many stops are in the database.
2.Find the id value for the stop 'Craiglockhart'.
3.Give the id and the name for the stops on the '4' 'LRT' service.
7.Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
8.Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
1.How many stops are in the database.
SELECT COUNT(id)
FROM stops
-- 直接计数即可
2.Find the id value for the stop 'Craiglockhart'.
SELECT id
FROM stops
WHERE name = 'Craiglockhart'
3.Give the id and the name for the stops on the '4' 'LRT' service.
数据跟答案一模一样,但是不知道为什么没有显示说是正确答案,无解
SELECT id, name
FROM stops JOIN route ON (id = stop)
WHERE num = 4 AND company = 'LRT'
4.The query shown gives the number of routes that visit either London Road (149) or Craiglockhart (53). Run the query and notice the two services that link these stops have a count of 2. Add a HAVING clause to restrict the output to these two routes.
SELECT company, num, COUNT(*)
FROM route
WHERE stop = 149 OR stop = 53
GROUP BY company, num
HAVING COUNT(stop = 149) = 2 AND COUNT(stop = 53) = 2
-- 直接筛选出经过2次149和53的行
5.Execute the self join shown and observe that b.stop gives all the places you can get to from Craiglockhart, without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.
SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
WHERE a.stop = 53 AND b.stop = 149
6.The query shown is similar to the previous one, however by joining two copies of the stops table we can refer to stops by name rather than by number. Change the query so that the services between 'Craiglockhart' and 'London Road' are shown. If you are tired of these places try 'Fairmilehead' against 'Tollcross'.
SELECT a.company, a.num, stopa.name, stopb.name
FROM route a JOIN route b ON
(a.company = b.company AND a.num = b.num)
JOIN stops stopa ON (a.stop = stopa.id)
JOIN stops stopb ON (b.stop = stopb.id)
WHERE stopa.name = 'Craiglockhart' AND stopb.name = 'London Road'
-- 与上一题类似,只不过这一题利用的是站名,而非编号
7.Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')
SELECT DISTINCT a.company, a.num
FROM route a JOIN route b ON
(a.company=b.company AND a.num=b.num)
WHERE a.stop = 115 AND b.stop = 137
-- DISTINCT 必须放在SELECT的第一个位置,否则就会报错
8.Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
SELECT a.company, a.num
FROM route a JOIN route b ON
(a.company = b.company AND a.num = b.num)
JOIN stops stopa ON (a.stop = stopa.id)
JOIN stops stopb ON (b.stop = stopb.id)
WHERE stopa.name = 'Craiglockhart' AND stopb.name = 'Tollcross'
-- 与第6题类似,都需要联结两个表,找出两个站之间的路线
9.Give a distinct list of the stops which may be reached from 'Craiglockhart' by taking one bus, including 'Craiglockhart' itself, offered by the LRT company. Include the company and bus no. of the relevant services.
SELECT stopb.name,a.company, a.num
FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)
JOIN stops stopa ON (a.stop = stopa.id)
JOIN stops stopb ON (b.stop = stopb.id)
WHERE stopa.name = 'Craiglockhart'
-- 过滤掉其中一个站为'Craiglockhart',SELECT的是另外一个站
10.Find the routes involving two buses that can go from Craiglockhart to Lochend.
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.
这一题真的很难啊!!!找了很多网上的解答,但是都搞不出来?!难道是网页的问题!!!无解!!!等之后基础 牢固了再来看看吧!!!如果大家有正解麻烦提供一下,感谢感谢感谢!!!
本文详细介绍了SQLZOO中关于Self Join的一系列挑战,包括查找数据库中的公交站数量,从Craiglockhart出发能到达的站点,以及从Craiglockhart到Lochend的换乘路线等复杂查询。内容涵盖了如何使用SQL查询连接不同站点的服务,并探讨了在解决这些查询时遇到的困难。

被折叠的 条评论
为什么被折叠?



