一张图看懂 SQL 的各类 JOIN 用法

发布日期:2019-07-23 19:42:55 阅读数: 652次 来源: 作者:

下图展现了 LEFT JOIN、RIGHT JOIN、INNER JOIN、OUTER JOIN 相关的 7 种用法。

亚博手机app

Inner JOIN

亚博手机app

SELECT <select_list>
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key

Left JOIN

亚博手机app

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key

Right JOIN

亚博手机app

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

Outer JOIN

亚博手机app

SELECT <select_list>
FROM Table_A A
亚博手机app FULL OUTER JOIN Table_B B
ON A.Key = B.Key

Left Excluding JOIN

亚博手机app

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

Right Excluding JOIN

亚博手机app

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL

Outer Excluding JOIN

亚博手机app

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
本文由亚博编辑整理亚博手机app