一張圖看懂SQL的各種Join用法
阿新 • • 發佈:2019-03-19
下圖展示了 LEFT JOIN、RIGHT JOIN、INNER JOIN、OUTER JOIN 相關的 7 種用法。
具體分解如下:
1、INNER JOIN(內連線)
- SELECT FROM Table_A AINNER JOIN Table_B BON A.Key = B.Key
2、LEFT JOIN(左連線)
- SELECT FROM Table_A ALEFT JOIN Table_B BON A.Key = B.Key
3、RIGHT JOIN(右連線)
- SELECT FROM Table_A ARIGHT JOIN Table_B BON A.Key = B.Key
4、OUTER JOIN(外連線)
- SELECT FROM Table_A AFULL OUTER JOIN Table_B BON A.Key = B.Key
5、LEFT JOIN EXCLUDING INNER JOIN(左連線-內連線)
- SELECT FROM Table_A ALEFT JOIN Table_B BON A.Key = B.KeyWHERE B.Key IS NULL
6.RIGHT JOIN EXCLUDING INNER JOIN(右連線-內連線)
- SELECT FROM Table_A ARIGHT JOIN Table_B BON A.Key = B.KeyWHERE A.Key IS NULL
7、OUTER JOIN EXCLUDING INNER JOIN(外連線-內連線)
- SELECT FROM Table_A AFULL OUTER JOIN Table_B BON A.Key = B.KeyWHERE A.Key IS NULL OR B.Key IS NULL