---
title: "左连接、右连接、交叉连接、全外连接"
author: "thurchin"
date: "2014-02-14T17:04:09Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2014-02-14T17:04:08Z"
id: 2017462
url: "https://thurchin.iteye.com/blog/2017462"
---

# [左连接、右连接、交叉连接、全外连接](http://www.cnblogs.com/LeoTerry/archive/2010/03/26/1696988.html)

 

**第一部分、连接查询**

一、内连接

内连接查询操作列出与连接条件匹配的数据行，它使用比较运算符比较被连接列的列值。内连接分三种：

1、等值连接：在连接条件中使用等于号(=)运算符比较被连接列的列值，其查询结果中列出被连接表中的所有列，包括其中的重复列。

2、不等连接： 在连接条件使用除等于运算符以外的其它比较运算符比较被连接的列的列值。这些运算符包括&gt;、&gt;=、&lt;=、&lt;、!&gt;、!&lt;和&lt;&gt;。

3、自然连接：在连接条件中使用等于(=)运算符比较被连接列的列值，但它使用选择列表指出查询结果集合中所包括的列，并删除连接表中的重复列。

 

二、外连接

返回到查询结果集合中的不仅包含符合连接条件的行，而且还包括左表(左外连接时)、右表(右外连接时)或两个边接表(全外连接)中的所有数据行。

 

三、交叉连接

交叉连接不带WHERE 子句，它返回被连接的两个表所有数据行的笛卡尔积，返回到结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。例，titles表中有6类图书，而publishers表中有8家出版社，则下列交叉连接检索到的记录数将等于6\*8=48行。

**第二部分、实例说明**

- 实例

Book表：![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/1.jpg) Student表：![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/2.jpg)

一、内连接

select *

from \[Book] as b,\[Student] as s

where b.StudentId=s.StudentId

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         等价于如下（也可以不要关键字inner，此为系统默认）

select *

from \[Book] as b inner join \[Student] as s

ON b.StudentId=s.StudentId

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         结果为：

![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/3.jpg)

 

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         执行过程

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

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         说明

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

 

二、外连接

1、左外连接

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         代码

select *

from \[Book] as b left join \[Student] as s

ON b.StudentId=s.StudentId

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         结果

 

![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/4.jpg)

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         执行过程

即以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、右外连接

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         代码

select *

from \[Book] as b right join \[Student] as s

ON b.StudentId=s.StudentId

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         结果

 

![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/5.jpg)

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         执行过程

即以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、全外连接

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         代码

select *

from \[Book] as b full outer join \[Student] as s

ON b.StudentId=s.StudentId

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         结果

 

![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/6.jpg)

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         执行过程

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

 

三、交叉连接

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         代码

select *

from \[Book] as b CROSS Join \[Student] as a

Order by b.BookId

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         结果

![](http://images.cnblogs.com/cnblogs_com/leoterry/SQL/%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2/7.jpg)

 

![*](http://www.cnblogs.com/LeoTerry/admin/file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gif)         执行过程

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