sqlzoo练习15-self join

连接查询包含3种方式:

  • 内联结
  • 外联结
  • 交叉联结

本文中学习的是自连接self-join。涉及到的两个表及其相关的字段

stops(id, name)
route(num, company, pos, stop)

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.

image

route表

A route is the path through town taken by a bus.

image

练习

所有的题目运行都出问题,提示表是非法,不存在,why?

image
  1. How many stops are in the database.
select count(stops.id)
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 id, name
from stops 
join route on stops.id=route.stop
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.

笔记:

  1. 显示了两条路线的总数
  2. 分组和过滤
  3. 要求:添加1个having子句来将结果限制成2条线路
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(53) to London Road(149).

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;

下面的部分需要重新做,参考了两个博主:

https://github.com/edsfocci/SQL-SQL_Zoo/blob/master/09_self_join.sql

https://github.com/jisaw/sqlzoo-solutions/blob/master/self-join.sql

  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')

列出连接两个站点的全部服务信息

select distinct a.company, a.num
from route a
    join route b on (a.num=b.num and a.company=b.company)
    join stops stopa on (a.stop=stopa.id)
    join stops stopb on (b.stop=stopb.id)
where a.stop=115
and b.stop=137;
  1. Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'
select distinct a.company, a.num
from route a
    join route b on (a.num=b.num and a.company=b.company)
    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 stopa.name, a.company, a.num
from route a
   join route b on (a.num=b.numa and a.company=b.company)
   join stops stopa on (a.stop=stopa.id)
   join stops stopa on (b.stop=stopb.id)
where stopb.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.
select stopa.name, stopb.name
from route a 
join route b on (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 = 'Sighthill';

self join quiz

  1. Select the code that would show it is possible to get from Craiglockhart to Haymarket
select distinct a.name, b.name
from stops a 
   join route z on a.id=z.stop
   join route y on y.num=z.num
   join stops b on y.stop=b.id
where a.name='Craiglockhart' and b.name='Haymarket';
  1. Select the code that shows the stops that are on route.num '2A' which can be reached with one bus from Haymarket?
select s2.id, s2.name, r2.company, r2.num
from stops s1, stops s2, route r1, route r2
where s1.name='Haymarket' and s1.id=r1.stop
and r1.company=r2.company 
and r1.num=r2.num
and r2.stop=r2.id
and r2.num='2A';
  1. Select the code that shows the services available from 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=b.stop)
join stops stopb on (b.stop=stopb.id)
where stopa.name='Tollcross';

demo

油管上看到的一个例子,讲解如何通过不同的方式实现自连接。

  • 内联结:inner join,忽略空行
  • 外联结:left/right/full join,保留空行;
  • 交叉联结:cross join,保留空行

Oracle数据库支持full join,mysql是不支持full join的

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272