sqlzoo--self join

news/2024/7/7 15:18:45 标签: sql
  • sqlzoo–select basics,select from world

  • sqlzoo–select from nobel ,select in select

  • sqlzoo–sum and count

  • sqlzoo–the join operation

  • sqlzoo–More JOIN operations

  • sqlzoo–using null

  • sqlzoo–self join

sqlzooself_join_9">sqlzoo–self join

已有字段:

table:stopstable:route
idnum
namecompany
pos
stop
  1. 数据库中有多少个站点

    sql">select count(id) from stops;
    
  2. 找出车站‘craiglockhart’的id

    sql">select id from stops
    where name = 'Craiglockhart';
    
  3. 列出巴士公司’LRT‘4号线的站点编号和站名

    sql">select id,name from route 
    join stops on (id = stop)
    where company = 'LRT' and num = 4;
    
  4. 查询途经London Road (149) 或 Craiglockhart (53)的巴士路线号码,有两条路线会经过这个站点两次,使用having语句列出这两条路线。

    sql">SELECT company, num, COUNT(*)
    FROM route WHERE stop=149 OR stop=53
    GROUP BY company,num
    HAVING count(*) = 2;
    
  5. 使用自连接来显示能够从Craiglockhart到 London Road直达的路线

    sql">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. 使用两个stops表来进行自连接,来显示Craiglockhart到 London Road的服务资料。

    sql">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. 列出连接115和137公司名称和路线号码,不要重复

    sql">select company,num from route where num in 
    (select  a.num from route as a,route as b 
    where a.stop != b.stop 
    and (a.company = b.company and a.num=b.num)
    and a.stop='115' and b.stop='137')
    group by num,company;
    
  8. 列出连接车站stops’Craiglockhart’ 到 ‘Tollcross’ 的公司名和路线号码

    sql">SELECT b.company, b.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. 不重复列出由 ‘Craiglockhart’ 乘一站到达的站点,包括其本身,列出站名,公司名以及路线号码

    sql">select  stopb.name,a.company,a.num
    from route as a join route as b 
    on (a.company=b.company and a.num=b.num)
    join stops as stopa on (stopa.id=a.stop)
    join stops as stopb on (stopb.id=b.stop)
    where stopa.name='Craiglockhart';
    
  10. Find the routes involving two buses that can go from Craiglockhart to Sighthill.
    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.

    sql">SELECT DISTINCT bus1.num, bus1.company, name, bus2.num, bus2.company 
    FROM(SELECT start1.num, start1.company, stop1.stop 
    FROM route AS start1 
    JOIN route AS 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 AS start2 
    JOIN route AS 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 = 'Sighthill')) AS bus2 ON bus1.stop = bus2.stop 
    JOIN stops ON bus1.stop = stops.id;
    
    

self join quiz

  1. 显示从Craiglockhart 到Haymarket的可能路线

    sql">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';
    
  2. 在路线2A上并且能够一站到达haymarket的站点

    sql">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=S2.id AND R2.num='2A';
    
  3. 到tollcross的可能服务路线

    sql">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='Tollcross';
    

http://www.niftyadmin.cn/n/1130369.html

相关文章

Linux上安装Apache环境及安装过程报错解决方案(零初始环境)

2019独角兽企业重金招聘Python工程师标准>>> Note:要从零开始搭建,就不要嫌中间遇到各种eggache的问题! 一.下载apache源代码 1.下载地址:http://httpd.apache.org/download.cgi 找稳定的最新的版本(Stabl…

ASP.NET 应用程序生命周期概览

本文描述应用程序生命周期的摘要信息,列表了重要的生命周期事件并描述如何编写合适的处理代码。在 ASP.NET 中,部分处理步骤只在应用程序初始化并处理请求时才会发生。另外,对来自浏览器的 ASP.NET 请求而提供服务仅是 Web 服务器架构的一小部…

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Wri…

父类不能转换成子类

父类不能转换成子类 Exception in thread "main" java.lang.ClassCastException: Person cannot be cast to Boyat Test.main(Test.java:5)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMe…

ansible安装和简单使用

一、安装1、安装第三方epel源centos 5的epelrpm -ivh http://mirrors.sohu.com/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpmcentos 6的epelrpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm由于是6版本所以安装6的epel2、安装ansibl…

Textarea高度随内容自适应地增长,无滚动条

<HTML> <HEAD> <meta http-equiv"Content-Type" content"text/html; charsetUTF-8"> <TITLE>枫芸志 文本框textarea高度自适应增长/伸缩</TITLE> </HEAD> <BODY><textarea id"txtContent" rows&q…

岛田庄司《占星术杀人魔法》读后感

昨天晚上夜谈的时候&#xff0c;聊到了少年包青天里的一个分尸案&#xff0c;今天查了查&#xff0c;叫《隐逸村案》&#xff0c;里面实用6个人的尸体拼出7个人的假象&#xff0c;立即就想到了《占星术杀人案》。其有用这个想法的小说还真不少&#xff0c;包青天里的应该是借鉴…

指定時間點js 倒計時間

指定時間點&#xff1a;<script type "text/javascript"><!--//functionGetRTime(){var EndTime new Date(2006,11,15,12,45); //截止时间:2006年6月10日0时0分var NowTime new Date();var nMS EndTime.getTime() - NowTime.getTime();var nD Math.floor(n…