SQL:OUTER JOIN用法詳解

來源:互聯網
上載者:User

標籤:outer join   sql   外串連   

SQL--JOIN用法外聯結。外聯結可以是左向外聯結、右向外聯結或完整外部聯結。 
在 FROM 子句中指定外聯結時,可以由下列幾組關鍵字中的一組指定:

LEFT JOIN 或 LEFT OUTER JOIN。 
左向外聯結的結果集包括 LEFT OUTER 子句中指定的匹配條件的行和左表的所有行。

RIGHT JOIN 或 RIGHT OUTER JOIN。 
右向外聯結是左向外聯結的反向聯結。將返回匹配條件的行和右表的所有行。

FULL JOIN 或 FULL OUTER JOIN。 
完整外部聯結返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的挑選清單列包含空值。如果表之間有匹配行,則整個結果集行包含基表的資料值。

內聯結 僅 當至少有一個同屬於兩表的行符合聯結條件時,內聯結才返回行。內聯結消除與另一個表中的任何行不匹配的行。而外聯結會返回 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 左向外聯結的查詢和結果:

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors a LEFT OUTER JOIN publishers p
   ON a.city = p.city
ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

下面是結果集:

au_fname             au_lname                       pub_name          
-------------------- ------------------------------ ----------------- 
Reginald             Blotchet-Halls                 NULL
Michel               DeFrance                       NULL
Innes                del Castillo                   NULL
Ann                  Dull                           NULL
Marjorie             Green                          NULL
Morningstar          Greene                         NULL
Burt                 Gringlesby                     NULL
Sheryl               Hunter                         NULL
Livia                Karsen                         NULL
Charlene             Locksley                       NULL
Stearns              MacFeather                     NULL
Heather              McBadden                       NULL
Michael              O‘Leary                        NULL
Sylvia               Panteley                       NULL
Albert               Ringer                         NULL
Anne                 Ringer                         NULL
Meander              Smith                          NULL
Dean                 Straight                       NULL
Dirk                 Stringer                       NULL
Johnson              White                          NULL
Akiko                Yokomoto                       NULL
Abraham              Bennet                         Algodata Infosystems
Cheryl               Carson                         Algodata Infosystems

(23 row(s) affected)

不管是否與 publishers 表中的 city 列匹配,LEFT OUTER JOIN 均會在結果中包含 authors 表的所有行。注意:結果中所列的大多數作者都沒有相匹配的右表資料,因此,這些行的 pub_name 列包含空值。

使用右向外聯結
假 設在 city 列上聯結 authors 表和 publishers 表。結果只顯示在出版商所在城市居住的作者(本例中為 Abraham Bennet 和 Cheryl Carson)。SQL-92 右向外聯結運算子 RIGHT OUTER JOIN 指明:不管第一個表中是否有匹配的資料,結果將包含第二個表中的所有行。

若要在結果中包括所有的出版商,而不管城市中是否還有出版商居住,請使用 SQL-92 右向外聯結。下面是 Transact-SQL 右向外聯結的查詢和結果:

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors AS a RIGHT OUTER JOIN publishers AS p
   ON a.city = p.city
ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

下面是結果集:

au_fname             au_lname                 pub_name             
-------------------- ------------------------ -------------------- 
Abraham              Bennet                   Algodata Infosystems
Cheryl               Carson                   Algodata Infosystems
NULL                 NULL                     Binnet & Hardley
NULL                 NULL                     Five Lakes Publishing
NULL                 NULL                     GGG&G
NULL                 NULL                     Lucerne Publishing
NULL                 NULL                     New Moon Books
NULL                 NULL                     Ramona Publishers
NULL                 NULL                     Scootney Books

(9 row(s) affected)

使用謂詞(如將聯結與常量比較)可以進一步限制外聯結。下例包含相同的右向外聯結,但消除銷售量低於 50 本的書籍的書名:

USE pubs
SELECT s.stor_id, s.qty, t.title
FROM sales s RIGHT OUTER JOIN titles t
   ON s.title_id = t.title_id
   AND s.qty > 50
ORDER BY s.stor_id ASC

下面是結果集:

stor_id qty    title                                                     
------- ------ --------------------------------------------------------- 
(null) (null) But Is It User Friendly?                                   
(null) (null) Computer Phobic AND Non-Phobic Individuals: Behavior 
            Variations                  
(null) (null) Cooking with Computers: Surreptitious Balance Sheets       
(null) (null) Emotional Security: A New Algorithm                        
(null) (null) Fifty Years in Buckingham Palace Kitchens                  
7066   75     Is Anger the Enemy?                                        
(null) (null) Life Without Fear                                          
(null) (null) Net Etiquette                                              
(null) (null) Onions, Leeks, and Garlic: Cooking Secrets of the 
            Mediterranean                  
(null) (null) Prolonged Data Deprivation: Four Case Studies              
(null) (null) Secrets of Silicon Valley                                  
(null) (null) Silicon Valley Gastronomic Treats                          
(null) (null) Straight Talk About Computers                              
(null) (null) Sushi, Anyone?                                             
(null) (null) The Busy Executive‘s Database Guide                        
(null) (null) The Gourmet Microwave                                      
(null) (null) The Psychology of Computer Cooking                         
(null) (null) You Can Combat Computer Stress!                            

(18 row(s) affected)

有關謂詞的更多資訊,請參見 WHERE。 

使用完整外部聯結
若 要通過在聯結結果中包括不匹配的行保留不匹配資訊,請使用完整外部聯結。Microsoft® SQL Server? 2000 提供完整外部聯結運算子 FULL OUTER JOIN,不管另一個表是否有匹配的值,此運算子都包括兩個表中的所有行。

假設在 city 列上聯結 authors 表和 publishers 表。結果只顯示在出版商所在城市居住的作者(本例中為 Abraham Bennet 和 Cheryl Carson)。SQL-92 FULL OUTER JOIN 運算子指明:不管表中是否有匹配的資料,結果將包括兩個表中的所有行。

若要在結果中包括所有作者和出版商,而不管城市中是否有出版商或者出版商是否住在同一個城市,請使用完整外部聯結。下面是 Transact-SQL 完整外部聯結的查詢和結果:

USE pubs
SELECT a.au_fname, a.au_lname, p.pub_name
FROM authors a FULL OUTER JOIN publishers p
   ON a.city = p.city
ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC

下面是結果集:

au_fname             au_lname                     pub_name             
-------------------- ---------------------------- -------------------- 
Reginald             Blotchet-Halls               NULL
Michel               DeFrance                     NULL
Innes                del Castillo                 NULL
Ann                  Dull                         NULL
Marjorie             Green                        NULL
Morningstar          Greene                       NULL
Burt                 Gringlesby                   NULL
Sheryl               Hunter                       NULL
Livia                Karsen                       NULL
Charlene             Locksley                     NULL
Stearns              MacFeather                   NULL
Heather              McBadden                     NULL
Michael              O‘Leary                      NULL
Sylvia               Panteley                     NULL
Albert               Ringer                       NULL
Anne                 Ringer                       NULL
Meander              Smith                        NULL
Dean                 Straight                     NULL
Dirk                 Stringer                     NULL
Johnson              White                        NULL
Akiko                Yokomoto                     NULL
Abraham              Bennet                       Algodata Infosystems
Cheryl               Carson                       Algodata Infosystems
NULL                 NULL                         Binnet & Hardley
NULL                 NULL                         Five Lakes Publishing
NULL                 NULL                         GGG&G
NULL                 NULL                         Lucerne Publishing
NULL                 NULL                         New Moon Books
NULL                 NULL                         Ramona Publishers
NULL                 NULL                         Scootney Books

(30 row(s) affected)

SQL:OUTER JOIN用法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.