系統城裝機大師 - 唯一官網:www.farandoo.com!

當前位置:首頁 > 數據庫 > Mysql > 詳細頁面

Mysql中常用的幾種join連接方式總結匯總

時間:2022-05-08來源:www.farandoo.com作者:電腦系統城

目錄
  • 1.內連接
  • 2.左連接
  • 3.右連接
  • 4.查詢左表獨有數據
  • 5.查詢右表獨有數據
  • 6.全連接
  • 7.查詢左右表各自的獨有的數據
  • 總結

1.首先準備兩張表

部門表:

d0d974a8979311953540d70a289e2332.png

員工表:

1258156d80964119fbacf7f4a6787f2c.png

以下我們就對這兩張表進行不同的連接操作

1.內連接

作用: 查詢兩張表的共有部分

語句:Select from tableA A Inner join tableB B on A.Key = B.Key

示例:SELECT * from employee e INNER JOIN department d on e.dep_id = d.id;

結果顯示:通過這個查找的方法,我們沒有查到id為8的數據

e80f31088c5aa791949c1bb62deec668.png

2.左連接

作用:把左邊表的內容全部查出,右邊表只查出滿足條件的記錄

語句:Select from tableA A Left Join tableB B on A.Key = B.Key

示例:SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id;

結果顯示:

76323cba2abd9453f769db4470a233c6.png

3.右連接

作用:把右邊表的內容全部查出,左邊表只查出滿足條件的記錄

語句:Select from tableA A Left Join tableB B on A.Key = B.Key

示例:SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id;

結果顯示:

34066aa04f51cd6e5c477827f893921b.png

4.查詢左表獨有數據

作用:查詢A的獨有數據

語句:Select from tableA A Left Join tableB B on A.Key = B.Key where B.key IS NULL

示例:SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id WHERE d.id IS NULL;

結果顯示:

f0dd519be53c7f69a3d3ac874a0caa85.png

5.查詢右表獨有數據

作用:查詢B的獨有數據

語句:Select from tableA A Right Join tableB B on A.Key = B.Key where A.key IS NULL

示例:SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id WHERE e.id IS NULL;

結果顯示:

025414646aec4dd3136847b0eba4bf5e.png

6.全連接

作用:查詢兩個表的全部信息

語句:Select from tableA A Full Outter Join tableB B on A.Key = B.Key

注:Mysql 默認不支持此種寫法 Oracle支持       可以使用將左連接與右連接結合起來作為全連接

示例:

1
2
3
SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id
UNION
SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id

結果顯示:

dfcec7a6ffe337dc488b946476f3cab8.png

7.查詢左右表各自的獨有的數據

作用:查詢A和B各自的獨有的數據

語句:Select from tableA A Full Outter Join tableB B on A.Key = B.Key where A.key = null or B.key=null

示例:

1
2
3
SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id WHERE d.id is NULL
UNION
SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id WHERE e.dep_id is NULL

結果顯示:

98d4622d4ecf409d325d19ec4dd6a0ac.png

總結

到此這篇關于Mysql中常用的幾種join連接方式的文章就介紹到這了

分享到:

相關信息

系統教程欄目

欄目熱門教程

人氣教程排行

站長推薦

熱門系統下載

jlzzjlzz亚洲乱熟在线播放