SQL 中各种 join 用法

INNER JOIN (内连接)

内连接:根据连接条件讲两张表的数据连接起来,如果没有符合的数据都会被过滤掉,留下符合条件的数据. -->INNER可以省略

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

LEFT JOIN(左连接)

左连接:显示两表中的所有列,展示左表的所有数据,若右表可以连接上则显示数据,关联不上显示为空

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

RIGHT JOIN(右连接)

右连接:显示两表中的所有列,展示右表的所有数据,若左表可以连接上则显示数据,关联不上显示为空

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

FULL JOIN(外连接)

外连接:显示两表所有的列,展示两表的所有数据,根据连接条件进行等值连接,找不到时,会在乡音位置显示为空.

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

CROSS JOIN(交叉连接或笛卡尔积连接)

交叉连接:笛卡尔积原则,结果为第一张表的数据乘以第二张表的数据

SELECT <select_list>
FROM Table_A 
CROSS  JOIN Table_B ;
或
SELECT <select_list>
FROM Table_A,Table_B ;

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;

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;

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;
posted @ 2020-03-24 11:51  ZHAIQ  阅读(1001)  评论(0编辑  收藏  举报