📄 6197.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MS SQL基础教程:数据库中的连接查询 - 编程入门网</title>
<meta name="keywords" content="MS SQL基础教程:数据库中的连接查询">
<meta name="description" content="MS SQL基础教程:数据库中的连接查询">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="/images/style.css" rel="stylesheet" type="text/css" />
<script src="/js1/head.js"></script>
</head>
<body leftmargin="0" topmargin="0" bgcolor="#efefef" oncopy=nocopy()>
<TABLE width="760" cellPadding="0" cellSpacing="0" bgcolor="#eff7fe" align="center">
<TR>
<TD><a href="/index.htm"><img src="/images/logo1.gif" width="150" height="60" border="0"></a></TD>
<TD width="470" align="right"><script src="/js1/top.js"></script></TD>
<TD width="125" align="center"><script src="/js1/topsy.js"></script></TD>
</TR>
</TABLE>
<table width="760" border="0" cellpadding="1" cellspacing="0" class="bklan" align="center">
<tr>
<td align="center" bgcolor="#eff7fe" height="24"> | <a href='/Programming/index.htm'>编程语言</a> | <a href='/webkf/index.htm'>web开发</a> | <a href='/data/index.htm'>数据库</a> | <a href='/Network/index.htm'>网络技术</a> | <a href='/OS/index.htm'>操作系统</a> | <a href='/Servers/index.htm'>服务器</a> | <a href='/web/index.htm'>网页设计</a> | <a href='/Design/index.htm'>图形设计</a> | <a href='/Office/index.htm'>办公软件</a> | <a href='/soft/index.htm'>常用软件</a> | <a href='/shadu/index.htm'>杀毒频道</a> | <a href='/PC/index.htm'>学电脑</a> |</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="760" align="center" bgcolor="#ffffff" border="0">
<tr>
<td align="center"><script src="/js1/content1.js"></script></td>
</tr>
</table>
<table width="760" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td height="25" background="/templets/img/31bg3.gif" align="left" class="guidet"> → 当前位置:<a href='http://www.bianceng.cn/'>首页</a>→<a href='/data/index.htm'>数据库</a>→<a href='/data/SQLServer/index.htm'>SQL Server</a>→<a href='/data/SQLServer/jc/index.htm'>SQL Server教程</a>→正文</td>
</tr>
</table>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td valign="top" class="guidet" width="595"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="ct">
<tr>
<td align="center" valign="middle" class="til"><h3> MS SQL基础教程:数据库中的连接查询 </h3>
发布时间:2007-12-25 来源:网络/责编:编程入门 作者:佚名<br />
<script src="/js1/468.js"></script>
</td>
</tr>
<tr>
<td style="font-size:14px" align="left" class="til"><table border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><script src="/js1/300.js"></script></td>
</tr>
</table><p> 数据库中的各个表中存放着不同的数据,用户往往需要用多个表中的数据来组合、提炼出所需要的信息。如果一个查询需要对多个表进行操作,就称为连接查询。连接查询的结果集或结果表,称为表之间的连接连接。查询实际上是通过各个表之间共同列的关联性来查询数据的,它是关系数据库查询最主要的特征。 </p>
<p> 连接查询分为等值连接查询、非等值连接查询、自连接查询、外部连接查询和复合条件连接查询。</p>
<p> 10.3.1 等值与非等值连接查询</p>
<p> 表之间的连接是通过相等的字段值连接起来的查询称为等值连接查询。可以用两种方式来指定连接条件。下面例子中的两个程序段运行结果相同。</p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614M25301RZ2.gif" /></p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614M360193052.gif" /></p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614M42F20K63.gif" /></p>
<p> <strong>注意:</strong>边续查询的连接条件中,各连接字段的数据类型不必相同,但必须相容。</p>
<p> 10.3.2 非等值连接查询</p>
<p> 在等值查询的连接条件中不使用等号,而使用其它比较运算符就构成了非等值连接查询。可以使用的比较运算符有:> 、>、=、 <、 <=、 !=, 还可以使用BETWEEN…AND 之类的谓词。---www.bianceng.cn</p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614M4D021Q45.gif" /></p>
<p> 显然,这个例子没有实际应用价值,同时也说明非等值连接查询往往需要同其它连接查询结合使用,尤其是同等值连接查询结合。其用法请参见后面介绍的复合条件连接查询。</p>
<p> 10.3.3 自连接查询</p>
<p> 连接不仅可以在表之间进行,也可以使一个表同其自身进行连接,这种连接称为自连</p>
<p> 接(Self Join),相应的查询称为自连接查询。</p>
<p> 例10-21: 查询在公司工作的工龄相同的员工。</p>
<p> use pangu</p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614M5250221331.gif" /></p>
<p> 10.3.4 外部连接查询</p>
<p> 在前面所举的例子中,连接的结果是从两个或两个以上的表的组合中挑选出符合连接条件的数据,如果数据无法满足连接条件则将其丢弃。通常称这种方法为内部连接(InnerJoin)。在内部连接中,参与连接的表的地位是平等的。与内部连接相对的方式称为外部连接(Outer Join)。在外部连接中,参与连接的表有主从之分,以主表的每行数据去匹配从表的数据列,符合连接条件的数据将直接返回到结果集中,对那些不符合连接条件的列,将被填上NULL 值后再返回到结果集中(对BIT 类型的列,由于BIT 数据类型不允许NULL 值,因此将会被填上0 值再返回到结果中)。</p>
<p> 外部连接分为左外部连接(Left Outer Join)和右外部连接(Right Outer Join)两种。以主表所在的方向区分外部连接,主表在左边,则称为左外部连接,主表在右边,则称为右外部连接。</p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614M5S0232444.gif" /></p>
<p> 10.3.5 复合条件连接查询</p>
<p> 在WHERE 子句中使用多个连接条件的查询,称为复合条件连接查询。</p>
<p> <img onclick="get_larger(this)" alt="" src="/upimg/071226/11b614MA0249309.gif" /></p>
<p> <a href="/data/SQLServer/jc/200712/6352.htm">查看全套"MS SQL入门基础教程"</a></p><center><br /><script src="/js1/4682.js"></script></center>
<p>上一篇:<a href='/data/SQLServer/jc/200712/6196.htm'>MS SQL基础教程:SELECT语句的应用</a> 下一篇:<a href='/data/SQLServer/jc/200712/6198.htm'>MS SQL基础教程:嵌套查询</a> </p></td>
</tr>
<tr>
<td align="left" style="font-size:14px;" height="25"><IMG src="/images/t0.gif" width="12" height="12" align=absMiddle><strong>相关文章</strong><br /><table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6196.htm">MS SQL基础教程:SELECT语句的应用</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6198.htm">MS SQL基础教程:嵌套查询</a><br/>
</td>
</tr>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6195.htm">MS SQL基础教程:数据查询-SELECT语句</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6199.htm">MS SQL基础教程:合并查询</a><br/>
</td>
</tr>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6194.htm">MS SQL基础教程:数据库缺省值</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6200.htm">MS SQL基础教程:存储查询结果</a><br/>
</td>
</tr>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6193.htm">MS SQL基础教程:数据库规则</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6201.htm">MS SQL基础教程:数据库全文检索</a><br/>
</td>
</tr>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6202.htm">MS SQL基础教程:删除数据</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6203.htm">MS SQL基础教程:更新数据</a><br/>
</td>
</tr>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6190.htm">改善SQL Server的内存管理</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6204.htm">MS SQL基础教程:SQL数据库中的事务</a><br/>
</td>
</tr>
<tr>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6205.htm">MS SQL基础教程:数据库中的锁</a><br/>
</td>
<td width='50%'>
·<a href="/data/SQLServer/jc/200712/6188.htm">SQL Server2000中的触发器使用</a><br/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="165" valign="top" class="guideb"><script src="/js1/1601.js"></script><table width="100%" border="0" cellpadding="0" cellspacing="0" valign="top">
<tr>
<td height="25" align="center" background="/templets/img/31bg3.gif"><strong>阅读排行</strong></td>
</tr>
<tr>
<td>·<a href="/data/SQLServer/jc/200712/6352.htm">MS SQL Server入门教程</a><br/>
·<a href="/data/SQLServer/jc/200807/10974.htm">MS SQL基础教程:存储过程</a><br/>
·<a href="/data/SQLServer/jc/200705/1189.htm">sql语言教程</a><br/>
·<a href="/data/SQLServer/jc/200807/10973.htm">MS SQL基础教程:创建存储</a><br/>
·<a href="/data/SQLServer/jc/200705/1182.htm">sql字符串函数</a><br/>
·<a href="/data/SQLServer/jc/200712/6301.htm">MS SQL基础教程:SQL Serve</a><br/>
·<a href="/data/SQLServer/jc/200712/6302.htm">MS SQL基础教程:SQL Serve</a><br/>
·<a href="/data/SQLServer/jc/200712/6314.htm">MS SQL基础教程:数据类型</a><br/>
·<a href="/data/SQLServer/jc/200705/1188.htm">Sql语言基础</a><br/>
·<a href="/data/SQLServer/jc/200712/6311.htm">MS SQL基础教程:Transact-</a><br/>
·<a href="/data/SQLServer/jc/200712/6312.htm">MS SQL基础教程:SQL变量</a><br/>
·<a href="/data/SQLServer/jc/200705/1184.htm">sql基本语句</a><br/>
·<a href="/data/SQLServer/jc/200807/10972.htm">MS SQL基础教程:管理存储</a><br/>
·<a href="/data/SQLServer/jc/200712/6306.htm">MS SQL基础教程:数据库基</a><br/>
·<a href="/data/SQLServer/jc/200705/1178.htm">sql数据表</a><br/>
</td>
</tr>
<tr>
<td><script src="/js1/1602.js"></script></td>
</tr>
<tr>
<td height="25" align="center" background="/templets/img/31bg3.gif"><strong>最新文章</strong></td>
</tr>
<tr>
<td><script src="/plus/js/0.js" language="javascript"></script></td>
</tr>
</table></td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="760" align="center" bgcolor="#ffffff" border="0">
<tr>
<td align="center"><script src="/js1/content2.js"></script></td>
</tr>
</table>
<table width="760" border="0" cellspacing="0" cellpadding="0" align="center">
<tr height="26">
<td bgcolor="#e1f0fd" width="48"></td>
<td bgcolor="#6ab3f4" width="35"></td>
<td bgcolor="#0a518f" colspan="2" width="4"></td>
<td bgcolor="#c0c0c0" width="530" align="center"><a title="将本站设为你的首页" onclick="this.style.behavior='url(#default#homepage)';this.sethomepage('http://www.bianceng.cn');return false;" href="http://www.bianceng.cn/">设为首页</a> | <a class="navmenu"
title="将本站加入到你的收藏夹"
href="javascript:window.external.AddFavorite(location.href,document.title)">加入收藏</a> | <a href="/about/about.htm">关于本站</a> | <a href="/plus/flink.php">友情链接</a> | <a href="/about/banquan.htm">版权声明</a> | <a href="/plus/sitemap.html">网站地图</a> | <a href="/plus/rssmap.html">RSS订阅</a></td>
<td bgcolor="#0a518f" colspan="2" width="4"></td>
<td bgcolor="#6ab3f4" width="32"></td>
<td bgcolor="#e1f0fd" width="47"></td>
</tr>
<tr height="26">
<td colspan="9" bgcolor="#FFFFFF" align="center">编程入门网 版权所有,bianceng.cn,All Rights Reserved. <script src="/js/tongji.js"></script> 阅读次数:<script src="/plus/count.php?aid=6197&mid=0" language="javascript"></script></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -