日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL内连接方法_Mysql常用的几种join连接方式

發布時間:2023/12/19 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL内连接方法_Mysql常用的几种join连接方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.首先準備兩張表

部門表:

員工表:

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

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的數據

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;

結果顯示:

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;

結果顯示:

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;

結果顯示:

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;

結果顯示:

6.全連接

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

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

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

示例:

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

結果顯示:

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

示例:

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

結果顯示:

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的MySQL内连接方法_Mysql常用的几种join连接方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。