您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何正確的使用mysql-joins方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
假設(shè)有兩個表,Table_A和Table_B。這兩個表中的數(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_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 ) //帶空號的table_reference join_condition: ON expression //on開頭的條件語句
這是最簡單、最容易理解的連接,也是最常見的連接。此查詢將返回左表(表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)
此查詢將返回左表(表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)
此查詢將返回左表(表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)
此查詢將返回右表(表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)
此查詢將返回右表(表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)
此聯(lián)接也可以稱為完全外聯(lián)接或完全聯(lián)接。此查詢將返回兩個表中的所有記錄,連接左表(表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)
此查詢將返回左表(表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)建了一個備忘單,您可以在需要時打印出來。如果您右鍵單擊下面的圖像并選擇“將目標(biāo)保存為.”,您將下載完整大小的圖像。
看完上述內(nèi)容,你們對如何正確的使用mysql-joins方法有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。