zoukankan      html  css  js  c++  java
  • inner join,outer join,left join,right join的区别

    外联接
    外联接可以是左向外联接、右向外联接或完整外部联接。
    在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:

    LEFT JOIN 或 LEFT OUTER JOIN。
    左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。

    RIGHT JOIN 或 RIGHT OUTER JOIN。
    右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。

    FULL JOIN 或 FULL OUTER JOIN。
    完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。

    例如:
    表a
    id              name
    1               a
    2               b
    3               c

    表b
    id              store
    1              15
    2              10
    4              67

    inner join :
    select * from a inner join b on a.id=b.id
    结果:(只显示id匹配的选项)
    id            name             id            store
    1              a                 1             15
    2              b                 2             10

    内连接还有以下写法:
    select * from a inner join b where a.id=b.id
    select * from a,b where a.id=b.id --连接查询的另一种写法
    select * from a as e,b as r where e.id=r.id --使用as定义别名,当表名很长时有用
    select * from a e,b r where e.id=r.id --定义别名时可以省掉as
    外连接分左外连接和右外连接
    左外连接:
    select * from a left outer join b on a.id=b.id
    或:
    select * from a left join b on a.id=b.id
    结果:(除了显示匹配记录,还显示a表中所有的记录)
    id            name            id            stroe
    1              a                 1             15
    2              b                 2             10
    3              c                \N             \N

    右外连接:
    select * from a right outer join b on a.id=b.id
    或:
    select * from a right join b on a.id=b.id
    结果:(除了显示匹配的记录,还显示右表中所有的记录)
    id            name            id            store
    1              a                1              15
    2              b                2              10
    \N            \N               4              67
  • 相关阅读:
    线程池示例(摘抄)
    Visual Studio 相关
    Linq 内连接和外连接(转载)
    asp.net mvc4 简单使用Autofac依赖注入小结
    jquery加载解析XML文件
    权限验证AuthorizeAttribute
    常用SQL Server规范集锦及优化
    linq to datatable 和lambda查询datatable
    ASP.NET 大文件下载的实现思路及代码
    分页存储过程
  • 原文地址:https://www.cnblogs.com/qiangshu/p/1610206.html
Copyright © 2011-2022 走看看