SQL 中各种 join 用法

INNER JOIN (内连接)

内连接:根据连接条件讲两张表的数据连接起来,如果没有符合的数据都会被过滤掉,留下符合条件的数据. -->INNER可以省略SELECT <select_list> FROM Table_A AINNER JOIN Table_B BON A.Key = B.Key;

LEFT JOIN(左连接)

左连接:显示两表中的所有列,展示左表的所有数据,若右表可以连接上则显示数据,关联不上显示为空SELECT <select_list>FROM Table_A ARIGHT JOIN Table_B BON A.Key = B.Key;

RIGHT JOIN(右连接)

右连接:显示两表中的所有列,展示右表的所有数据,若左表可以连接上则显示数据,关联不上显示为空SELECT <select_list>FROM Table_A ARIGHT JOIN Table_B BON A.Key = B.Key;

FULL JOIN(外连接)

外连接:显示两表所有的列,展示两表的所有数据,根据连接条件进行等值连接,找不到时,会在乡音位置显示为空.SELECT <select_list>FROM Table_A AFULL JOIN Table_B BON 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 ALEFT JOIN Table_B BON A.Key = B.KeyWHERE B.Key IS NULL;

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

SELECT <select_list>FROM Table_A ARIGHT JOIN Table_B BON A.Key = B.KeyWHERE A.Key IS NULL;

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

SELECT <select_list>FROM Table_A AFULL OUTER JOIN Table_B BON A.Key = B.KeyWHERE A.Key IS NULL OR B.Key IS NULL;

相关文章