mssql中sql各种联合查询

php中文网
发布: 2016-06-07 17:48:58
原创
1941人浏览过

本文章介绍了关于sql中的所有联合查询,包括left join ,right join ,INNER JOIN 等查询,有需要学习的朋友可以参考一下本文章。

在sql server中,我们经常能用到连接,今天总结一下连接的基础知识。
连接的分类:

  • 交叉连接CROSS JOIN
  • 内连接INNER JOIN
  • 外连接{左外连接LEFT [OUTER] JOIN ;右外连接RIGHT [OUTER] JOIN;全外连接full [outer] join}
  • 自连接

以下通过例子来了解各个连接的异同点:

有两张表Teacher表和Course表:

交叉连接:

1.如果不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积;

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy5450')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5450><pre class="brush:php;toolbar:false;"> * from Course cross join Teacher
登录后复制
结果为:

由此结果可知,它的结果与 SELECT * FROM Course,Teacher 的结果相同。

2.如果有WHERE子句的话,往往会先生成两个表行数乘积的数据表然后才根据WHERE条件从中选择。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy6388')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6388><pre class="brush:php;toolbar:false;">1. select * from Course,Teacher where Course.T#=Teacher.T#<br />2. select * from Course cross join Teacher where Course.T#=Teacher.T#<font size="2"><strong><span>  (注:cross join后加条件只能用where,不能用on)</span></strong><br /></font>3. select * from Course inner join Teacher on Course.T#=Teacher.T#
登录后复制

结果为:

一般情况下,在效率上,Where可能具有和Inner join一样的效率,但是,在多表连接时,我们并不推荐使用where语句。
所以如果可以选择,我们最好使用语句3,有时使用Join语句可以帮助检查语句中的无效或者误写的关联条件。

内连接

内连接表示两边表同时符合条件的组合,就相当于普通的CROSS JOIN,只是格式不一样,
INNER JOIN在后面有一个ON子句(相当于WHERE)的搜索条件,用于过滤返回的行。
内连接没有笛卡尔积那么复杂要先生成行数乘积的数据表,所以内连接的效率要高于笛卡尔积的交叉连接。

外连接

指定条件的内连接,仅仅返回符合连接条件的条目。
外连接则不同,返回的结果不仅包含符合连接条件的行,而且包括左表(左外连接时), 右表(右连接时)或者两边连接(全外连接时)的所有数据行。

1)左外连接LEFT [OUTER] JOIN
显示符合条件的数据行,同时显示左边数据表不符合条件的数据行,右边没有对应的条目显示NULL

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy4158')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4158><pre class="brush:php;toolbar:false;">select * from Course left outer join Teacher on Course.T#=Teacher.T#
登录后复制
结果为:

2)右外连接RIGHT [OUTER] JOIN
显示符合条件的数据行,同时显示右边数据表不符合条件的数据行,左边没有对应的条目显示NULL

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy6069')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6069><pre class="brush:php;toolbar:false;">select * from Course right outer join Teacher on Course.T#=Teacher.T#
登录后复制
结果为:

3)全外连接full [outer] join

显示符合条件的数据行,同时显示左右不符合条件的数据行,相应的左右两边显示NULL,即显示左连接、右连接和内连接的并集。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy3355')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3355><pre class="brush:php;toolbar:false;">select * from Course full outer join Teacher on Course.T#=Teacher.T#
登录后复制
结果为:

Playground AI
Playground AI

AI图片生成和修图

Playground AI 108
查看详情 Playground AI

自连接

其实,在Sql Server中,我们还经常用到一种连接——自连接。
通过以下的例子,来了解自连接:
表树形结构表tb_TestTreeView

解决问题: 树形层次结构显示
/*
 这是一个地区表,里面存放了地区名及其所属上级地区,假设现在需要查询出各地区及其所属上级地区。
*/

自连接的方法1:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy8683')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8683><pre class="brush:php;toolbar:false;">select [Name] as '地区名',<br />  (select [Name] from tb_TestTreeView as a<br />    where a.ID = b.Parent ) as '上级地区名'<br />from tb_TestTreeView as b
登录后复制

自连接的方法2:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy7579')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7579><pre class="brush:php;toolbar:false;">select a.[Name] as '地区名',<br />       b.[Name] as '上级地区名'<br />from tb_TestTreeView as a<br />left join tb_TestTreeView as b<br />     on a.Parent = b.ID
登录后复制

结果为:

自连接三级(左联接):

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy1699')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1699><pre class="brush:php;toolbar:false;">select a.[Name] as '地区名',<br />       b.[Name] as '上级地区名',<br />       c.[Name] as '上上级地区名'<br />from tb_TestTreeView as a<br />left join tb_TestTreeView as b<br />     on a.Parent = b.ID <br />left join tb_TestTreeView as c<br />     on b.parent=c.id
登录后复制

结果为:

自连接三级(内联接):

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy5441')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5441><pre class="brush:php;toolbar:false;">select a.[Name] as '地区名',<br />       b.[Name] as '上级地区名',<br />       c.[Name] as '上上级地区名'<br />from tb_TestTreeView as a<br />inner join tb_TestTreeView as b<br />     on a.Parent = b.ID <br />inner join tb_TestTreeView as c<br />     on b.parent=c.id
登录后复制

结果为:

自连接四级(左链接):

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy1909')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1909><pre class="brush:php;toolbar:false;">select a.[Name] as '地区名',<br />       b.[Name] as '上级地区名',<br />       c.[Name] as '上上级地区名',<br />       d.[Name] as '上上上级地区名'<br />from tb_TestTreeView as a<br />left join tb_TestTreeView as b<br />     on a.Parent = b.ID <br />left join tb_TestTreeView as c<br />     on b.parent=c.id<br />left join tb_TestTreeView as d<br />     on c.parent=d.id
登录后复制

结果为:

自连接四级(内链接):

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy4985')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4985><pre class="brush:php;toolbar:false;">select a.[Name] as '地区名',<br />       b.[Name] as '上级地区名',<br />       c.[Name] as '上上级地区名',<br />       d.[Name] as '上上上级地区名'<br />from tb_TestTreeView as a<br />inner join tb_TestTreeView as b<br />     on a.Parent = b.ID<br />inner join tb_TestTreeView as c<br />     on b.Parent = c.ID<br />inner join tb_TestTreeView as d<br />     on c.Parent = d.ID
登录后复制

结果为:

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号