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相同时,重复输出。

  • 相关阅读:
    LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
    LeetCode 216. 组合总和 III(Combination Sum III)
    LeetCode 179. 最大数(Largest Number)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)
    指针变量、普通变量、内存和地址的全面对比
    MiZ702学习笔记8——让MiZ702变身PC的方法
    你可能不知道的,定义,声明,初始化
    原创zynq文章整理(MiZ702教程+例程)
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/5561018.html
Copyright © 2011-2022 走看看