zoukankan      html  css  js  c++  java
  • 表联接

    表1:teacher
    +----+--------+
    | id | name   |
    +----+--------+
    |  1 | 刘德华 |
    |  2 | 张学友 |
    |  4 | 黎明   |
    +----+--------+
    表2:student
    +----+------+--------+
    | id | name | tea_id |
    +----+------+--------+
    |  1 | 张三 |      1 |
    |  2 | 李四 |      1 |
    |  3 | 王五 |      1 |
    |  4 | 赵六 |      2 |
    |  5 | 孙七 |      2 |
    +----+------+--------+


    1.内连接:在每个表中找出符合条件的共有记录。[x inner join y on...]
    第一种写法:(相当于多表查询,用的是where!)
    select t.*,s.* from teacher t,student s where t.id=s.tea_id;
    第二种写法:(inner省略了)
    select t.*,s.* from teacher t join student s on t.id=s.tea_id;
    第三种写法:
    select t.*,s.* from teacher t inner join student s on t.id=s.tea_id;


    +----+--------+----+------+--------+
    | id | name   | id | name | tea_id |
    +----+--------+----+------+--------+
    |  1 | 刘德华 |  1 | 张三 |      1 |
    |  1 | 刘德华 |  2 | 李四 |      1 |
    |  1 | 刘德华 |  3 | 王五 |      1 |
    |  2 | 张学友 |  4 | 赵六 |      2 |
    |  2 | 张学友 |  5 | 孙七 |      2 |
    +----+--------+----+------+--------+


    2.外连接有三种方式:左连接,右连接和全连接。
    2.1 左连接:根据左表的记录,在被连接的右表中找出符合条件的记录与之匹配
    找不到与左表匹配的,用null表示。[x left [outer] join y on...]
    第一种写法:
    select t.*,s.* from teacher t left join student s on t.id=s.tea_id;
    第二种写法:
    select t.*,s.* from teacher t left outer join student s on t.id=s.tea_id;


    +----+--------+------+------+--------+
    | id | name   | id   | name | tea_id |
    +----+--------+------+------+--------+
    |  1 | 刘德华 |    1 | 张三 |      1 |
    |  1 | 刘德华 |    2 | 李四 |      1 |
    |  1 | 刘德华 |    3 | 王五 |      1 |
    |  2 | 张学友 |    4 | 赵六 |      2 |
    |  2 | 张学友 |    5 | 孙七 |      2 |
    |  4 | 黎明   | NULL | NULL |   NULL |
    +----+--------+------+------+--------+


    2.2 右连接:根据右表的记录,在被连接的左表中找出符合条件的记录与之匹配,
    找不到匹配的,用null填充。[x right [outer] join y on...]
    第一种写法:
    select t.*,s.* from teacher t right join student s on t.id=s.tea_id;
    第二种写法:
    select t.*,s.* from teacher t right outer join student s on t.id=s.tea_id;


    +------+--------+----+------+--------+
    | id   | name   | id | name | tea_id |
    +------+--------+----+------+--------+
    |    1 | 刘德华 |  1 | 张三 |      1 |
    |    1 | 刘德华 |  2 | 李四 |      1 |
    |    1 | 刘德华 |  3 | 王五 |      1 |
    |    2 | 张学友 |  4 | 赵六 |      2 |
    |    2 | 张学友 |  5 | 孙七 |      2 |
    +------+--------+----+------+--------+


    2.3 全连接:返回符合条件的所有表的记录,没有与之匹配的,用null表示(结果是左连接和右连接的并集)
    第一种写法:
    select t.*,s.* from teacher t full join student s on t.id=s.tea_id;
    第二种写法:
    select t.*,s.* from teacher t full outer join student s on t.id=s.tea_id;


    3 交叉连接(结果是笛卡尔积)
    select t.*,s.* from teacher t cross join student s;
    等效于:
    select t.*,s.* from teacher t,student s;
    +----+--------+----+------+--------+
    | id | name   | id | name | tea_id |
    +----+--------+----+------+--------+
    |  1 | 刘德华 |  1 | 张三 |      1 |
    |  2 | 张学友 |  1 | 张三 |      1 |
    |  4 | 黎明   |  1 | 张三 |      1 |
    |  1 | 刘德华 |  2 | 李四 |      1 |
    |  2 | 张学友 |  2 | 李四 |      1 |
    |  4 | 黎明   |  2 | 李四 |      1 |
    |  1 | 刘德华 |  3 | 王五 |      1 |
    |  2 | 张学友 |  3 | 王五 |      1 |
    |  4 | 黎明   |  3 | 王五 |      1 |
    |  1 | 刘德华 |  4 | 赵六 |      2 |
    |  2 | 张学友 |  4 | 赵六 |      2 |
    |  4 | 黎明   |  4 | 赵六 |      2 |
    |  1 | 刘德华 |  5 | 孙七 |      2 |
    |  2 | 张学友 |  5 | 孙七 |      2 |
    |  4 | 黎明   |  5 | 孙七 |      2 |
    +----+--------+----+------+--------+


    5自连接:连接的表都是同一个表,同样可以由内连接,外连接各种组合
    方式,按实际应用去组合。
    SELECT a.*,b.* FROM table_1 a,table_1 b WHERE a.[name]=b.[name] --连接的两表是同一个表,别称不一样




    SELECT a.*,b.* FROM table_1 a LEFT JOIN table_1 b ON a.[name]=b.[name] --左连接写法

  • 相关阅读:
    [NOI2015]程序自动分析
    D-query(莫队)
    小B的询问
    组合的输出 (dfs+记忆化)
    组合的输出 (dfs+记忆化)
    5719: 集合的划分(dfs)
    Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置
    Find Min In Rotated Sorted Array2,包含重复数字的反转序列找最小值。
    Find Min In Rotated Sorted Array,寻找反转序列中最小的元素。
    Search In Rotated SortedArray2, 有重复数据的反转序列。例如13111.
  • 原文地址:https://www.cnblogs.com/yaoxiaodan/p/5285723.html
Copyright © 2011-2022 走看看