zoukankan      html  css  js  c++  java
  • sql server join联结

    join学习起来有点乱,现做如下整理:

    table A

    id abc
    1 a
    2 b
    3 c
    4 d

    table B

    id abc
    1 e
    2 a
    3 f
    4 c

        --join或者inner join (内连接、等值连接):只返回两个表中连接字段相等的行。

      --select * from  A inner join B on A.abc=B.abc

    id abc id1 abc1
    1 a 2 a
    3 c 4 c

      --left join(左连接) :返回A表中所有的记录以及B表中连接字段相等的记录,没有匹配的则以null值取代。

      --select * from  A left join B on A.abc=B.abc

    id abc id1 abc1
    1 a 2 a
    2 b null null
    3 c 4 c
    4 d null null

      --right join(右连接):返回B表中所有的记录以及A表中连接字段相等的记录,没有匹配的则以null值取代。

      --select * from  A right join B on A.abc=B.abc

    id abc id1 abc1
    null null 1 e
    1 a 2 a
    null null 3 f
    2 c 4 c

       --full join(外连接、全连接):返回两个表中的行:left join + right join。对于没有匹配的记录,则会以null做为值。

      --select * from  A full join B on A.abc=B.abc

    Venn diagram of SQL cartesian join        

    id abc id1 abc1
    1 a 2 a
    2 b null null
    3 c 4 c
    4 d null null
    null null 1 e
    null null 3 f

        --先按照A表中的记录一条一条到B表找匹配项,没有则用null代替。

        --再按照B表中的记录一条一条到A表找匹配项,没有则用null代替。

        --排除重复的记录,则为最后结果

      --cross join:结果是笛卡尔积,就是第一个表的行数乘以第二个表的行数。

     
    要么生,要么死
  • 相关阅读:
    6.Ray-消息订阅器编写
    附录:2-Event Sourcing pattern (事件溯源设计模式)
    附录:1-Grain生命周期-译注
    4.Ray-Handler之CoreHandler编写
    Q&A-20180128
    TODO
    3.Ray-Event编写
    缓存技术内部交流_01_Ehcache3简介
    Spring AMQP 源码分析 04
    Spring AMQP 源码分析 03
  • 原文地址:https://www.cnblogs.com/llljpf/p/6510546.html
Copyright © 2011-2022 走看看