zoukankan      html  css  js  c++  java
  • 内连接、左外连接、右外连接、全外连接、交叉连接

    一、内连接

    也称等值连接,返回两张表都满足条件的部分

      select * from [Book] as b,[Student] as s where b.StudentId=s.StudentId

    等价于如下(也可以不要关键字inner,此为系统默认)

      select * from [Book] as b inner join [Student] as s ON b.StudentId=s.StudentId

    执行过程

      相当于内连接的向右连接。以from  [Book] inner join [Student]等式右边为基准,即以Student表(等式右表,s表)的s.StudentId为基准,遍历Book表(等式左表,Book表)中与之匹配的b.StudentId,然后拼接返回。结果含有重复的列,b.StudentId和s.StudentId。

    说明

      这与where b.StudentId=s.StudentId或者s.StudentId=b.StudentId位置没有关系。它仅仅代表满足条件而已,不判定谁为基准。以下外连接,交叉连接相同操作。 

    二、外连接

    1、左外连接

    左边的表的全部右边的表按条件,符合的显示,不符合则显示null

      select * from [Book] as b left join [Student] as s ON b.StudentId=s.StudentId

    执行过程

      即以from [Book] left join [Student]的Book表为基准,即以Book表(b表)的b.StudentId为基准。遍历Student表(s表)中与之匹配的b.StudentId。若b.StudentId含有s.StudentId匹配项,则进行拼接,然后遍历Student表的下一条s.StudentId,当查询完毕则进入下一条b.StudentId。若b.StudentId没有相应s.StudentId匹配项时,则显示左表的项,拼接右表的项显示为NULL。

    2、右外连接

    右边的表的全部左边的表按条件,符合的显示,不符合则显示null

      select * from [Book] as b right join [Student] as s ON b.StudentId=s.StudentId

    执行过程

      即以from [Book] right join [Student]的Student表为基准,即以Student表(s表)的s.StudentId为基准。遍历Book表(b表)中与之匹配的s.StudentId。若s.StudentId含有b.StudentId匹配项,则进行拼接,然后遍历Book表的下一条b.StudentId,当查询完毕则进入下一条s.StudentId。若s.StudentId没有相应b.StudentId匹配项时,则显示右表的项,拼接左表的项显示为NULL。

    3、全外连接

      select * from [Book] as b full outer join [Student] as s ON b.StudentId=s.StudentId

    执行过程

      即以from [Book] full outer join [Student]中先以Book表进行左外连接,然后以Student表进行右外连接

    三、交叉连接

    返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接也称作笛卡尔积

      select * from [Book] as b CROSS Join [Student] as a Order by b.BookId

    执行过程

      即是按照Order排序的Id,把要Join的右表无条件拼接过来。这样依次执行,这样这种记录便为两个表的记录的笛卡尔积。

  • 相关阅读:
    Cola:一个分布式爬虫框架
    MichaelBoselowitz/pygit2-examples: Examples of some "porcelain" git commands implemented with python bindings (pygit2) to the libgit2 library.
    https://github.com/mlzboy/spider-impl.git
    Installation and Status — CFFI 1.5.2 documentation
    CENTOS 6.5 安装 Python 2.7 总结
    CENTOS 6.5 安装 Python 2.7 总结
    Create a commit using pygit2
    Create a commit using pygit2
    Create a commit using pygit2
    LindDotNetCore~docker里图像上生成中文乱码问题
  • 原文地址:https://www.cnblogs.com/guanghe/p/13594654.html
Copyright © 2011-2022 走看看