• mysql学习09--自连接查询


    Edinburgh Buses

    This database consists of two tables: stops and routes

    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.

    FieldTypeNotes
    id INTEGER Arbitrary value
    name CHAR(30) The name of an area served by at least one bus

    route

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

    FieldTypeNotes
    num CHAR(5) The number of the bus - as it appears on the front of the vehicle. Oddly these numbers often include letters
    company CHAR(3) Several bus companies operate in Edinburgh. The main one is Lothian Region Transport - LRT
    pos INTEGER This indicates the order of the stop within the route. Some routes may revisit a stop. Most buses go in both directions.
    stop INTEGER This references the stops table

    As different companies use numbers arbitrarily the num and the company are both required to identify a route.

    1.How many stops are in the database.

    select count(*) 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. 

    Some of the data is incorrect.

    select id,name from stops join route
    on stops.id=route.stop
    where num='4' and company='LRT'
    order by id

    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(*)=2

    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

    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'

    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.

    Some of the data is incorrect.

    SELECT distinct stopa.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' or stopb.name='Craiglockhart') and a.company='LRT'

    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

  • 相关阅读:
    Python基础-16生成器-迭代器
    Python基础-15模块-包-库
    Python基础-14异常处理
    Python基础-13面向对象
    Python基础-12解析式
    Python 入门日记(十)—— 文件和异常
    Python 入门日记(九)—— 类
    Python 入门日记(八)—— 函数
    Python 入门日记(七)—— 用户输入和 while 循环
    Python 入门日记(六)—— 字典
  • 原文地址:https://www.cnblogs.com/asenyang/p/14352233.html
Copyright © 2020-2023  润新知