zoukankan      html  css  js  c++  java
  • sql 入门经典(第五版) Ryan Stephens 学习笔记  第四部分:建立复杂的数据库查询/

    第十三章: 在查询表里结合表

     1、等值结合 :  

    // 选择 tabla_a 和table_b 中id相等的行,输出 他们的id 和name

      select table_a.id , table_a.name , table_b.id , table_b.name from table_a , table_b where table_a.id = table_b.id ;

      或者使用 inner join 

      select table_a.id , table_a.name , table_b.id ,table_b.name from table_a inner join table_b on table_a.id = table_b.id;

      或者使用别名

      select a.id , a.name , b.id ,b.name from table_a  a ,table_b  b where a.id = b.id and a.salary > 2000;

    2、不等值结合

      与等值结合语法相同,只是将 等号改为 不等号但是不等值结合会输出多余的信息,因为 table A中每行数据都要和 table B中每一行数据进行比较 。

    若a.id != b.id ,则输出 相应的信息。

    3、外部结合

    4、自结合

      select a.last_name , a.first_name ,b.first_name , b.last_name from table_a  a,table_a b where a.last_name = b.last_name ;  

    应用:  在一个保存了 雇员标示号码,姓名,雇员主管标示号码的表里。

      select * from emp;

            id     name  mgr_id

            1  john      0

            2   mary    1

            3   steve       1

            4   jack     2

            5   sue    2

      现在需要找到 雇员与 雇主的关系

      select  e1.name , e2.name from emp e1,emp e2 where e1.mgr_id = e2.id ;

        name   name

        mary  john

        steve  john

        jack   mary

        sue   mary

     inner join / left join / right join 可以参考 

       也可以参考 http://www.cnblogs.com/xxlhcjh/p/4309982.html

    第十四章: 使用子查询定义未确定的数据

    1、 子查询与select 结合使用

      select colum from table where column where  columnb=(select colum_name from ...);

    2、   子查询与inset结合使用

      insert table1 select column1 from table2 where cloumn2>(select ......);

      将表二中满足条件的某几项 插入到表1 中,注: 插入的项数 = table1的column数

    3、 子查询与 update 结合使用

    4、 子查询与delete 结合使用

    第十五章: 组合多个查询

    1、 union 

      select id from stu union select id from jobe ;        // 当两个table中id相同时,不重复输出。

    2、 union all

      select id from stu union all select id from jobe ;    // 当两个table中id相同时,重复输出。

  • 相关阅读:
    OpenDaylight二层转发机制实验
    OpenvSwitch的GRE、Vxlan隧道
    Open vSwitch实验
    实验五 RYU控制器基本应用
    实验四 POX控制器编程实验指导
    实验二 OpenFlow应用实践
    SDN实验三 OpenFlow协议分析
    SDN实验一:mininet应用实践
    序列幂次求和的快速计算
    luogu P1409 骰子 题解
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/5561018.html
Copyright © 2011-2022 走看看