SQL RIGHT JOIN 右外部連接
上篇已介紹LEFT JOIN子句。而 RIGHT JOIN 右外部連接則為相反查詢
RIGHT JOIN 可以用來建立右外部連接,查詢的 SQL 敘述句 RIGHT JOIN 右側資料表 (table_B) 的所有記錄都會加入到查詢結果中,即使左側資料表 (table_A) 中的連接欄位沒有符合的值也一樣。有符合的值也一樣。
SELECT C.CustomerName, O.Order_No FROM table_A as C RIGHT JOIN table_ as O ON C.CustomerID=O.CustomerID;
CustomerID |
CompanyName |
CustomerName |
Address |
City |
PostalCode |
Country |
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
21 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitucion 2222 |
Mexico D.F. |
05021 |
Mexico |
31 |
Antonio Moreno Taqueria |
Antonio Moreno |
Mataderos 2312 |
Mexico D.F. |
05023 |
Mexico |
OrderID |
CustomerID |
OrderDate |
1001 |
1 |
1999-10-10 |
1002 |
2 |
1999-11-11 |
1003 |
3 |
1999-12-12 |
1004 |
1 |
1999-12-12 |
1005 |
31 |
1999-12-12 |
1006 |
31 |
2000-01-01 |
結果:
CustomerName |
OrderID |
Maria Anders |
1001 |
|
1002 |
|
1003 |
Maria Anders |
1004 |
Antonio Moreno |
1005 |
Antonio Moreno |
1006 |