一张图看懂 SQL 的各种 join 用法!


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

图片

具体分解如下:

1.INNER JOIN(内连接)

图片

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

2.LEFT JOIN(左连接)

图片

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

3.RIGHT JOIN(右连接)

图片

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

4.OUTER JOIN(外连接)

图片

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

5.LEFT JOIN EXCLUDING INNER JOIN(左连接-内连接)

图片

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

6.RIGHT JOIN EXCLUDING INNER JOIN(右连接-内连接)

图片

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

7.OUTER JOIN EXCLUDING INNER JOIN(外连接-内连接)

图片

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

关于join,你学废了么?


真诚地邀请您加入我们的大家庭.
在这里不仅有技术知识分享,还有博主们之间的互帮互助
不定期发红包,每月更有抽奖环节,游戏机和实体书相赠(包邮)
让我们抱团取暖,抱团内卷.打造美好C站.期待您的加入.
备注 : CSDN-xxxxxx (xxxxxx代表你csdn的昵称)

猜你喜欢

转载自blog.csdn.net/qq_44590469/article/details/126039238
今日推荐