sqlzoo练习16-self-join
连接查询包含3
种方式:
- 内联结
- 外联结
- 交叉联结
本文中学习的是自连接self-join
。涉及到的两个表及其相关的字段
1 | stops(id, name) |
stops表
This is a list of areas served by buses. The detail does not really include each actual bus stop - just areas within Edinburgh and whole towns near Edinburgh.
route表
A route is the path through town taken by a bus.
练习
所有的题目运行都出问题,提示表是非法,不存在,why?
- How many stops are in the database.
1 | select count(stops.id) |
- Find the id value for the stop ‘Craiglockhart’
1 | select id |
- Give the id and the name for the stops on the ‘4’ ‘LRT’ service.
1 | select id, name |
-
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.
笔记:
- 显示了两条路线的总数
- 分组和过滤
- 要求:添加1个having子句来将结果限制成2条线路
1 | select company, num, count(*) |
-
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(53) to London Road(149).
1 | select a.company, a.num, a.stop, b.stop |
下面的部分需要重新做,参考了两个博主:
https://github.com/edsfocci/SQL-SQL_Zoo/blob/master/09_self_join.sql
https://github.com/jisaw/sqlzoo-solutions/blob/master/self-join.sql
-
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’
1 | select a.company, a.num, stopa.name, stopb.name |
- Give a list of all the services which connect stops 115 and 137 (‘Haymarket’ and ‘Leith’)
列出连接两个站点的全部服务信息
1 | select distinct a.company, a.num |
- Give a list of the services which connect the stops ‘Craiglockhart’ and ‘Tollcross’
1 | select distinct a.company, a.num |
- 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.
1 | select stopa.name, a.company, a.num |
- 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.
1 | select stopa.name, stopb.name |
self join quiz
- Select the code that would show it is possible to get from Craiglockhart to Haymarket
1 | select distinct a.name, b.name |
- Select the code that shows the stops that are on route.num ‘2A’ which can be reached with one bus from Haymarket?
1 | select s2.id, s2.name, r2.company, r2.num |
- Select the code that shows the services available from Tollcross?
1 | select a.company, a.num, stopa.name, stopb.name |
demo
油管上看到的一个例子,讲解如何通过不同的方式实现自连接。
- 内联结:
inner join
,忽略空行 - 外联结:
left/right/full join
,保留空行; - 交叉联结:
cross join
,保留空行
Oracle数据库支持full join,mysql是不支持full join的