• inner join on, left join on, right join on


    1.定义

    inner join(等值连接) : 仅仅返回两个表中联结字段相等的记录

    left join(左联接) :返回包含左表中的全部记录和右表中联结字段相等的记录

    right join(右联接) :返回包含右表中的全部记录和左表中联结字段相等的记录

    INNER JOIN 语法: 

    INNER JOIN 连接两个数据表的使用方法:

    SELECT * FROM 表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号


    2.实例

    表A记录例如以下:
    aID               a Num   
    1                  a20050111
    2                  a20050112
    3                  a20050113
    4                  a20050114
    5                  a20050115

    表B记录例如以下:
    bID               bName
    1                   2006032401
    2                  2006032402
    3                  2006032403
    4                  2006032404
    8                  2006032408


    实验例如以下:
    1.left join

    sql语句例如以下: 
    select * from A
    left join B 
    on A.aID = B.bID

    结果例如以下:
    aID               aNum                          bID                  bName
    1                   a20050111                1                      2006032401
    2                   a20050112                2                     2006032402
    3                   a20050113                3                     2006032403
    4                   a20050114                4                     2006032404
    5                   a20050115                NULL              NULL
    (所影响的行数为 5 行)

    结果说明:
                   left join是以A表的记录为基础的,A能够看成左表,B能够看成右表,left join是以左表为准的.
    换句话说,左表(A)的记录将会所有表示出来,而右表(B)仅仅会显示符合搜索条件的记录(样例中为: A.aID = B.bID).
    B表记录不足的地方均为NULL.

    2.right join
    sql语句例如以下: 
    select * from A
    right join B 
    on A.aID = B.bID
    结果例如以下:
    aID               aNum                          bID                  bName
    1                   a20050111                1                      2006032401
    2                   a20050112                2                     2006032402
    3                   a20050113                3                     2006032403
    4                   a20050114                4                     2006032404
    NULL           NULL                          8                     2006032408
    (所影响的行数为 5 行)
    结果说明:
            细致观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.


    3.inner join
    sql语句例如以下: 
    select * from A
    innerjoin B 
    on A.aID = B.bID

    结果例如以下:
    aID               aNum                          bID                  bName
    1                   a20050111                1                      2006032401
    2                   a20050112                2                     2006032402
    3                   a20050113                3                     2006032403
    4                   a20050114                4                     2006032404

    结果说明:
            非常明显,这里仅仅显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它仅仅显示符合条件的记录.  还有就是inner join 能够结合where语句来使用 如:   select * from A innerjoin B on A.aID = B.bID where b.bname='2006032401' 这种话 就仅仅会放回一条数据了

  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4372105.html
Copyright © 2020-2023  润新知