SQL练习5-SQLZoo第9章 Self join

636 阅读2分钟

1. Introduction

地址sqlzoo.net/wiki/Self_j…

表结构
关于公共汽车站台的表

表名 字段名
stops id, name
route num, company, pos, stop

知识点统计

题号 知识点
5-10 自联结

2. Q & A

  1. How many stops are in the database.
SELECT count(*)
    FROM stops
  1. Find the id value for the stop 'Craiglockhart'.
SELECT id 
    FROM stops
    WHERE name= 'Craiglockhart'
  1. Give the id and the name for the stops on the '4' 'LRT' service.

可以通过联结两张表:

SELECT stops.* 
    FROM stops JOIN route ON (stops.id=route.stop)
    WHERE route.num=4 AND company='LRT'

也可以通过子查询:

SELECT * FROM stops
    WHERE id IN (
        SELECT stop FROM route 
            WHERE num = '4' AND company = 'LRT')
  1. 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(*)=2
  1. 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
  1. 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'
  1. Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith').

通过 id 查询:

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

通过站名查询:

SELECT DISTINCT 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='Haymarket'  AND stopb.name = 'Leith'
  1. 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'
  1. 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 DISTINCT bstop.name, a.company, a.num 
    FROM route a JOIN route b 
        ON (a.company = b.company AND a.num = b.num)
    JOIN stops astop ON (a.stop = astop.id)
	JOIN stops bstop ON (b.stop = bstop.id)
	WHERE astop.name = 'Craiglockhart'

  1. 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.

Hint:
Self-join twice to find buses that visit Craiglockhart and Sighthill, then join those on matching stops.

SELECT DISTINCT bus1.num, bus1.company, name, bus2.num, bus2.company 
    FROM (SELECT start1.num, start1.company, stop1.stop 
            FROM route start1 JOIN route stop1 
                ON (start1.num = stop1.num AND start1.company = stop1.company AND start1.stop != stop1.stop)
            WHERE start1.stop = (SELECT id FROM stops WHERE name = 'Craiglockhart')) AS bus1
    JOIN (SELECT start2.num, start2.company, start2.stop 
            FROM route start2 JOIN route stop2 
                ON (start2.num = stop2.num AND start2.company = stop2.company AND start2.stop != stop2.stop)
            WHERE stop2.stop = (SELECT id FROM stops WHERE name = 'Lochend')) AS bus2
    ON bus1.stop = bus2.stop
    JOIN stops ON bus1.stop = stops.id

两个route(start1和stop1) 合并,并且剔除出发站和抵达站为同一站的点

得到 'Craiglockhart'的经过所有站点、车次,不包含自己

两个route(start2和stop2)合并,并且剔除出发站和抵达站为同一站的点

得到'Lochend'的经过所有站点、车次,不包含自己

找到经过两个站的车次

通过BUS1与Stops 取得关联

最后一题参考:zhuanlan.zhihu.com/p/38488508