目錄
- Join 語法:
- 1、Inner JOIN: (內(nèi)連接)
- 2、Left JOIN: (左連接)
- 3、Left Excluding JOIN: (左連接排除內(nèi)連接結(jié)果)
- 4、Right JOIN: (右連接)
- 5、Right Excluding JOIN: (右連接排除內(nèi)連接結(jié)果)
- 6、Outer JOIN: (外連接)
- 7、Outer Excluding JOIN: (外連接排除內(nèi)連接結(jié)果)
JOIN對(duì)于接觸過數(shù)據(jù)庫(kù)的人,這個(gè)詞都不陌生,而且很多人很清楚各種JOIN,還有很多人對(duì)這個(gè)理解也不是很透徹。
假設(shè)我們有兩個(gè)表,Table_A和Table_B。這兩個(gè)表中的數(shù)據(jù)如下所示:
TABLE_A | TABLE_B
PK Value | PK Value
---- ---------- | ---- ----------
1 FOX | 1 TROT
2 COP | 2 CAR
3 TAXI | 3 CAB
6 WASHINGTON | 6 MONUMENT
7 DELL | 7 PC
5 ARIZONA | 8 MICROSOFT
4 LINCOLN | 9 APPLE
10 LUCENT | 11 SCOTCH
Join 語法:
join_table:
table_reference JOIN table_factor [join_condition] //內(nèi)連接
| table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference join_condition //外連接
| table_reference LEFT SEMI JOIN table_reference join_condition //左半連接
| table_reference CROSS JOIN table_reference [join_condition] (as of Hive 0.10)
table_reference:
table_factor //表
| join_table //join語句
table_factor:
tbl_name [alias] //表名[別名]
| table_subquery alias //子查尋[別名]
| ( table_references ) //帶空號(hào)的table_reference
join_condition:
ON expression //on開頭的條件語句
1、Inner JOIN: (內(nèi)連接)
這是最簡(jiǎn)單、最容易理解的連接,也是最常見的連接。此查詢將返回左表(表A)中具有右表(表B)中匹配記錄的所有記錄。此連接寫成如下:
SELECT select_list>
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key
-- Inner JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
INNER JOIN Table_B B
ON A.PK = B.PK
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
1 FOX TROT 1
2 COP CAR 2
3 TAXI CAB 3
6 WASHINGTON MONUMENT 6
7 DELL PC 7
(5 row(s) affected)
2、Left JOIN: (左連接)
此查詢將返回左表(表A)中的所有記錄,而不管這些記錄是否與右表(表B)中的任何記錄匹配。它還將從正確的表中返回任何匹配的記錄。此連接寫成如下:
SELECT select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
-- Left JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
1 FOX TROT 1
2 COP CAR 2
3 TAXI CAB 3
4 LINCOLN NULL NULL
5 ARIZONA NULL NULL
6 WASHINGTON MONUMENT 6
7 DELL PC 7
10 LUCENT NULL NULL
(8 row(s) affected)
3、Left Excluding JOIN: (左連接排除內(nèi)連接結(jié)果)
此查詢將返回左表(表A)中與右表(表B)中的任何記錄都不匹配的所有記錄。此連接寫成如下:
SELECT select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL
-- Left Excluding JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK
WHERE B.PK IS NULL
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
4 LINCOLN NULL NULL
5 ARIZONA NULL NULL
10 LUCENT NULL NULL
(3 row(s) affected)
4、Right JOIN: (右連接)
此查詢將返回右表(表B)中的所有記錄,而不管這些記錄中是否有任何記錄與左表(表A)中的記錄相匹配。它還將返回左表中的任何匹配記錄。此連接寫成如下:
SELECT select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
-- Right JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
1 FOX TROT 1
2 COP CAR 2
3 TAXI CAB 3
6 WASHINGTON MONUMENT 6
7 DELL PC 7
NULL NULL MICROSOFT 8
NULL NULL APPLE 9
NULL NULL SCOTCH 11
(8 row(s) affected)
5、Right Excluding JOIN: (右連接排除內(nèi)連接結(jié)果)
此查詢將返回右表(表B)中與左表(表A)中的任何記錄都不匹配的所有記錄。此連接寫成如下:
SELECT select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL
-- Right Excluding JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
NULL NULL MICROSOFT 8
NULL NULL APPLE 9
NULL NULL SCOTCH 11
(3 row(s) affected)
6、Outer JOIN: (外連接)
此聯(lián)接也可以稱為完全外聯(lián)接或完全聯(lián)接。此查詢將返回兩個(gè)表中的所有記錄,連接左表(表A)中與右表(表B)中的記錄相匹配的記錄。此連接寫成如下:
SELECT select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
-- Outer JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
1 FOX TROT 1
2 COP CAR 2
3 TAXI CAB 3
6 WASHINGTON MONUMENT 6
7 DELL PC 7
NULL NULL MICROSOFT 8
NULL NULL APPLE 9
NULL NULL SCOTCH 11
5 ARIZONA NULL NULL
4 LINCOLN NULL NULL
10 LUCENT NULL NULL
(11 row(s) affected)
7、Outer Excluding JOIN: (外連接排除內(nèi)連接結(jié)果)
此查詢將返回左表(表A)中的所有記錄和右表(表B)中不匹配的所有記錄。我還不需要使用這種類型的聯(lián)接,但所有其他類型的聯(lián)接我都相當(dāng)頻繁地使用。此連接寫成如下:
SELECT select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL OR B.Key IS NULL
-- Outer Excluding JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL
OR B.PK IS NULL
A_PK A_Value B_Value B_PK
---- ---------- ---------- ----
NULL NULL MICROSOFT 8
NULL NULL APPLE 9
NULL NULL SCOTCH 11
5 ARIZONA NULL NULL
4 LINCOLN NULL NULL
10 LUCENT NULL NULL
(6 row(s) affected)
注意,在外部聯(lián)接上,首先返回內(nèi)部連接記錄,然后返回右連接記錄,最后返回左連接記錄(至少,我的Microsoft SQL Server就是這樣做的;當(dāng)然,這不需要使用任何ORDERBY語句)。您可以訪問維基百科文章以獲得更多信息(但是,條目不是圖形化的)。我還創(chuàng)建了一個(gè)備忘單,您可以在需要時(shí)打印出來。如果您右鍵單擊下面的圖像并選擇“將目標(biāo)保存為.”,您將下載完整大小的圖像。
到此這篇關(guān)于mysql-joins具體用法說明的文章就介紹到這了,更多相關(guān)mysql-joins用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- sql server 交集,差集的用法詳解
- SQL語句的并集UNION 交集JOIN(內(nèi)連接,外連接)等介紹
- MySQL實(shí)現(xiàn)差集(Minus)和交集(Intersect)測(cè)試報(bào)告
- MySQL 查詢結(jié)果取交集的實(shí)現(xiàn)方法
- SQLServer中求兩個(gè)字符串的交集