使用外联接

仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行。内联接消除与另一个表中的任何行不匹配的行。而外联接会返回 FROM 子句中提到的至少一个表或视图的所有行,只要这些行符合任何 WHERE 或 HAVING 搜索条件。将检索通过左向外联接引用的左表的所有行,以及通过右向外联接引用的右表的所有行。完整外部联接中两个表的所有行都将返回。

Microsoft® SQL Server™ 2000 对在 FROM 子句中指定的外联接使用以下 SQL-92 关键字:

  • LEFT OUTER JOIN 或 LEFT JOIN

  • RIGHT OUTER JOIN 或 RIGHT JOIN

  • FULL OUTER JOIN 或 FULL JOIN

SQL Server 支持 SQL-92 外联接语法,以及在 WHERE 子句中使用 *= 和 =* 运算符指定外联接的旧式语法。由于 SQL-92 语法不容易产生歧义,而旧式 Transact-SQL 外联接有时会产生歧义,因此建议使用 SQL-92 语法。

使用左向外联接

假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。

若要在结果中包括所有的作者,而不管出版商是否住在同一个城市,请使用 SQL-92 左向外联接。下面是 Transact-SQL 左向外联接的查询和结果:

<font color="#000000">USE pubs<br />SELECT a.au_fname, a.au_lname, p.pub_name<br />FROM authors a LEFT OUTER JOIN publishers p<br />   ON a.city = p.city<br />ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC<br /></font></pre>
<p><font color="#000000">下面是结果集:</font></p><pre><font color="#000000">au_fname             au_lname                       pub_name          <br />-------------------- ------------------------------ ----------------- <br />Reginald             Blotchet-Halls                 NULL<br />Michel               DeFrance                       NULL<br />Innes                del Castillo                   NULL<br />Ann                  Dull                           NULL<br />Marjorie             Green                          NULL<br />Morningstar          Greene                         NULL<br />Burt                 Gringlesby                     NULL<br />Sheryl               Hunter                         NULL<br />Livia                Karsen                         NULL<br />Charlene             Locksley                       NULL<br />Stearns              MacFeather                     NULL<br />Heather              McBadden                       NULL<br />Michael              O'Leary                        NULL<br />Sylvia               Panteley                       NULL<br />Albert               Ringer                         NULL<br />Anne                 Ringer                         NULL<br />Meander              Smith                          NULL<br />Dean                 Straight                       NULL<br />Dirk                 Stringer                       NULL<br />Johnson              White                          NULL<br />Akiko                Yokomoto                       NULL<br />Abraham              Bennet                         Algodata Infosystems<br />Cheryl               Carson                         Algodata Infosystems<br /><br />(23 row(s) affected)<br /></font></pre>
<p><font color="#000000">不管是否与 <b>publishers</b> 表中的 <b>city </b>列匹配,LEFT OUTER JOIN 均会在结果中包含 
<b>authors </b>表的所有行。注意:结果中所列的大多数作者都没有相匹配的数据,因此,这些行的 <b>pub_name</b> 列包含空值。</font></p>
<h5><font color="#000000">使用右向</font><font style="background-color: rgb(49, 106, 197);" color="#000000">外联接</font></h5>
<p><font color="#000000">假设在 <b>city</b> 列上联接 <b>authors</b> 表和 <b>publishers</b> 
表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。SQL-92 右向</font><font style="background-color: rgb(49, 106, 197);" color="#000000">外联接</font><font color="#000000">运算符 RIGHT OUTER JOIN 
指明:不管第一个表中是否有匹配的数据,结果将包含第二个表中的所有行。</font></p>
<p><font color="#000000">若要在结果中包括所有的出版商,而不管城市中是否还有出版商居住,请使用 SQL-92 右向</font><font style="background-color: rgb(49, 106, 197);" color="#000000">外联接</font><font color="#000000">。下面是 Transact-SQL 
右向</font><font style="background-color: rgb(49, 106, 197);" color="#000000">外联接</font><font color="#000000">的查询和结果:</font></p><pre><font color="#000000">USE pubs<br />SELECT a.au_fname, a.au_lname, p.pub_name<br />FROM authors AS a RIGHT OUTER JOIN publishers AS p<br />   ON a.city = p.city<br />ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC<br /></font></pre>
<p><font color="#000000">下面是结果集:</font></p><pre><font color="#000000">au_fname             au_lname                 pub_name             <br />-------------------- ------------------------ -------------------- <br />Abraham              Bennet                   Algodata Infosystems<br />Cheryl               Carson                   Algodata Infosystems<br />NULL                 NULL                     Binnet & Hardley<br />NULL                 NULL                     Five Lakes Publishing<br />NULL                 NULL                     GGG&G<br />NULL                 NULL                     Lucerne Publishing<br />NULL                 NULL                     New Moon Books<br />NULL                 NULL                     Ramona Publishers<br />NULL                 NULL                     Scootney Books<br /><br />(9 row(s) affected)<br /></font></pre>
<p><font color="#000000">使用谓词(如将联接与常量比较)可以进一步限制</font><font style="background-color: rgb(49, 106, 197);" color="#000000">外联接</font><font color="#000000">。下例包含相同的右向</font><font style="background-color: rgb(49, 106, 197);" color="#000000">外联接</font><font color="#000000">,但消除销售量低于 50 本的书籍的书名:</font></p><pre><font color="#000000">USE pubs<br />SELECT s.stor_id, s.qty, t.title<br />FROM sales s RIGHT OUTER JOIN titles t<br />   ON s.title_id = t.title_id<br />   AND s.qty > 50<br />ORDER BY s.stor_id ASC<br /></font></pre>
<p><font color="#000000">下面是结果集:</font></p><pre><font color="#000000">stor_id qty    title                                                     <br />------- ------ --------------------------------------------------------- <br />(null) (null) But Is It User Friendly?                                   <br />(null) (null) Computer Phobic AND Non-Phobic Individuals: Behavior <br />            Variations                  <br />(null) (null) Cooking with Computers: Surreptitious Balance Sheets       <br />(null) (null) Emotional Security: A New Algorithm                        <br />(null) (null) Fifty Years in Buckingham Palace Kitchens                  <br />7066   75     Is Anger the Enemy?                                        <br />(null) (null) Life Without Fear                                          <br />(null) (null) Net Etiquette                                              <br />(null) (null) Onions, Leeks, and Garlic: Cooking Secrets of the <br />            Mediterranean                  <br />(null) (null) Prolonged Data Deprivation: Four Case Studies              <br />(null) (null) Secrets of Silicon Valley                                  <br />(null) (null) Silicon Valley Gastronomic Treats                          <br />(null) (null) Straight Talk About Computers                              <br />(null) (null) Sushi, Anyone?                                             <br />(null) (null) The Busy Executive's Database Guide                        <br />(null) (null) The Gourmet Microwave                                      <br />(null) (null) The Psychology of Computer Cooking                         <br />(null) (null) You Can Combat Computer Stress!                            <br /><br />(18 row(s) affected)<br /></font></pre>
<p><font color="#000000">有关谓词的更多信息,请参见 <a href="javascript:hhobj_1.Click()">WHERE</a>。 </font></p>
<h5><font color="#000000">使用完整外部联接</font></h5>
<p><font color="#000000">若要通过在联接结果中包括不匹配的行保留不匹配信息,请使用完整外部联接。Microsoft® SQL Server™ 2000 提供完整外部联接运算符 
FULL OUTER JOIN,不管另一个表是否有匹配的值,此运算符都包括两个表中的所有行。</font></p>
<p><font color="#000000">假设在 <b>city</b> 列上联接 <b>authors</b> 表和 <b>publishers</b> 
表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。SQL-92 FULL OUTER JOIN 
运算符指明:不管表中是否有匹配的数据,结果将包括两个表中的所有行。</font></p>
<p><font color="#000000">若要在结果中包括所有作者和出版商,而不管城市中是否有出版商或者出版商是否住在同一个城市,请使用完整外部联接。下面是 Transact-SQL 
完整外部联接的查询和结果:</font></p><pre><font color="#000000">USE pubs<br />SELECT a.au_fname, a.au_lname, p.pub_name<br />FROM authors a FULL OUTER JOIN publishers p<br />   ON a.city = p.city<br />ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC<br /></font></pre>
<p><font color="#000000">下面是结果集:</font></p><pre><font color="#000000">au_fname             au_lname                     pub_name             <br />-------------------- ---------------------------- -------------------- <br />Reginald             Blotchet-Halls               NULL<br />Michel               DeFrance                     NULL<br />Innes                del Castillo                 NULL<br />Ann                  Dull                         NULL<br />Marjorie             Green                        NULL<br />Morningstar          Greene                       NULL<br />Burt                 Gringlesby                   NULL<br />Sheryl               Hunter                       NULL<br />Livia                Karsen                       NULL<br />Charlene             Locksley                     NULL<br />Stearns              MacFeather                   NULL<br />Heather              McBadden                     NULL<br />Michael              O'Leary                      NULL<br />Sylvia               Panteley                     NULL<br />Albert               Ringer                       NULL<br />Anne                 Ringer                       NULL<br />Meander              Smith                        NULL<br />Dean                 Straight                     NULL<br />Dirk                 Stringer                     NULL<br />Johnson              White                        NULL<br />Akiko                Yokomoto                     NULL<br />Abraham              Bennet                       Algodata Infosystems<br />Cheryl               Carson                       Algodata Infosystems<br />NULL                 NULL                         Binnet & Hardley<br />NULL                 NULL                         Five Lakes Publishing<br />NULL                 NULL                         GGG&G<br />NULL                 NULL                         Lucerne Publishing<br />NULL                 NULL                         New Moon Books<br />NULL                 NULL                         Ramona Publishers<br />NULL                 NULL                         Scootney Books<br /><br />(30 row(s) affected)<br /></font>
posted on 2008-08-22 10:39  ayajenson  阅读(232)  评论(0编辑  收藏  举报