zoukankan      html  css  js  c++  java
  • SQLServer 4类join查询:inner join,left join, right join, full outer join

    1.from A inner join B on A.ID=B.ID :两表都有的记录才列出

    A表:  ID   Name                           B表: ID  Class

             1      BMW                                   1     C1

             2      Aodi                                    2     C2

             Null    Benz                                  4     Null

             3        Null                                   Null   C4

    select  * from A inner join B on A.ID=B.ID:

                ID       Name            ID        Class

                1        BMW              1         C1

                2         Aodi              2         C2

    2.from A left join B on A.ID=B.ID:

       A表中所有记录列出,B中无法匹配的用Null匹配

    A表:  ID   Name                           B表: ID  Class

             1      BMW                                   1     C1

             2      Aodi                                    2     C2

             Null    Benz                                  4     Null

             3        Null                                   Null   C4

    select  * from A left join B on A.ID=B.ID:

                ID       Name            ID        Class

                1        BMW             1          C1

                2        Aodi              2          C2

               Null      Benz             Null       Null  

                3        Null               Null        Null        

     

    3.from A right join B on A.ID=B.ID:

      B表中所有记录列出,A中无法匹配的用Null匹配

    A表:  ID   Name                           B表: ID  Class

             1      BMW                                   1     C1

             2      Aodi                                    2     C2

             Null    Benz                                  4     Null

             3        Null                                   Null   C4

    select  * from A right join B on A.ID=B.ID:

            ID       Name                   ID        Class

           1          BMW                     1           C1

           2         Aodi                       2           C2

           Null      Null                        4           Null

           Null      Null                       Null        C4

    4.from A full outer join B on A.ID=B.ID.

       列出A表所有记录,B表所有记录。无法匹配的用Null替代

    A表:  ID   Name                           B表: ID  Class

             1      BMW                                   1     C1

             2      Aodi                                    2     C2

             Null    Benz                                  4     Null

             3        Null                                   Null   C4

    select * from A full outer join B on A.ID=B.ID.

    ID       Name                   ID        Class

      1      BMW                     1         C1

      2      Aodi                      2        C2

      Null   Benz                     Null     Null

      3       Null                       Null    Null

     Null     Null                       4        Null

     Null    Null                        Null      C4

    其它join查询:

      (1) left outer join,等价于left join.

     (2)right outer join, 等价于right join.

      (3) cross join,    笛卡尔乘积查询

  • 相关阅读:
    [转]深入探析c# Socket
    [转]软件架构师书籍
    常指针类型讲解(const int *p,int*const p,int const *p,指针常量,常量指针)
    使用CreateFile()打开COM10及以上串口的问题[转并整理]
    如何在MFC中打开控制台[转]
    vb.net相关的除法运算
    计算机如何表示小数
    精简指令集与复杂指令集
    【转】ArcSDE数据被锁定后的解锁方法
    [ERR0134] Requested Service is not available【转载】
  • 原文地址:https://www.cnblogs.com/imihiroblog/p/2943744.html
Copyright © 2011-2022 走看看