MySQL多表查詢實(shí)例詳解【鏈接查詢、子查詢等】
本文實(shí)例講述了MySQL多表查詢。分享給大家供大家參考,具體如下:
準(zhǔn)備工作:準(zhǔn)備兩張表,部門表(department)、員工表(employee)
create table department( id int, name varchar(20) ); create table employee( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int );
#插入數(shù)據(jù) insert into department values (200,'技術(shù)'), (201,'人力資源'), (202,'銷售'), (203,'運(yùn)營(yíng)'); insert into employee(name,sex,age,dep_id) values ('egon','male',18,200), ('alex','female',48,201), ('wupeiqi','male',38,201), ('yuanhao','female',28,202), ('nvshen','male',18,200), ('xiaomage','female',18,204) ;
# 查看表結(jié)構(gòu)和數(shù)據(jù) mysql> desc department; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.19 sec)
mysql> desc employee; +--------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-----------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | sex | enum('male','female') | NO | | male | | | age | int(11) | YES | | NULL | | | dep_id | int(11) | YES | | NULL | | +--------+-----------------------+------+-----+---------+----------------+ 5 rows in set (0.01 sec)
mysql> select * from department; +------+--------------+ | id | name | +------+--------------+ | 200 | 技術(shù) | | 201 | 人力資源 | | 202 | 銷售 | | 203 | 運(yùn)營(yíng) | +------+--------------+ 4 rows in set (0.02 sec)
mysql> select * from employee; +----+----------+--------+------+--------+ | id | name | sex | age | dep_id | +----+----------+--------+------+--------+ | 1 | egon | male | 18 | 200 | | 2 | alex | female | 48 | 201 | | 3 | wupeiqi | male | 38 | 201 | | 4 | yuanhao | female | 28 | 202 | | 5 | nvshen | male | 18 | 200 | | 6 | xiaomage | female | 18 | 204 | +----+----------+--------+------+--------+ 6 rows in set (0.00 sec)
ps:觀察兩張表,發(fā)現(xiàn)department表中id=203部門在employee中沒(méi)有對(duì)應(yīng)的員工,發(fā)現(xiàn)employee中id=6的員工在department表中沒(méi)有對(duì)應(yīng)關(guān)系。
一多表鏈接查詢
SELECT 字段列表
FROM 表1 INNER|LEFT|RIGHT JOIN 表2
ON 表1.字段 = 表2.字段;
(1)先看第一種情況交叉連接:不適用任何匹配條件。生成笛卡爾積.--->重復(fù)最多
mysql> select * from employee,department; +----+----------+--------+------+--------+------+--------------+ | id | name | sex | age | dep_id | id | name | +----+----------+--------+------+--------+------+--------------+ | 1 | egon | male | 18 | 200 | 200 | 技術(shù) | | 1 | egon | male | 18 | 200 | 201 | 人力資源 | | 1 | egon | male | 18 | 200 | 202 | 銷售 | | 1 | egon | male | 18 | 200 | 203 | 運(yùn)營(yíng) | | 2 | alex | female | 48 | 201 | 200 | 技術(shù) | | 2 | alex | female | 48 | 201 | 201 | 人力資源 | | 2 | alex | female | 48 | 201 | 202 | 銷售 | | 2 | alex | female | 48 | 201 | 203 | 運(yùn)營(yíng) | | 3 | wupeiqi | male | 38 | 201 | 200 | 技術(shù) | | 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 | | 3 | wupeiqi | male | 38 | 201 | 202 | 銷售 | | 3 | wupeiqi | male | 38 | 201 | 203 | 運(yùn)營(yíng) | | 4 | yuanhao | female | 28 | 202 | 200 | 技術(shù) | | 4 | yuanhao | female | 28 | 202 | 201 | 人力資源 | | 4 | yuanhao | female | 28 | 202 | 202 | 銷售 | | 4 | yuanhao | female | 28 | 202 | 203 | 運(yùn)營(yíng) | | 5 | nvshen | male | 18 | 200 | 200 | 技術(shù) | | 5 | nvshen | male | 18 | 200 | 201 | 人力資源 | | 5 | nvshen | male | 18 | 200 | 202 | 銷售 | | 5 | nvshen | male | 18 | 200 | 203 | 運(yùn)營(yíng) | | 6 | xiaomage | female | 18 | 204 | 200 | 技術(shù) | | 6 | xiaomage | female | 18 | 204 | 201 | 人力資源 | | 6 | xiaomage | female | 18 | 204 | 202 | 銷售 | | 6 | xiaomage | female | 18 | 204 | 203 | 運(yùn)營(yíng) |
(2)內(nèi)連接:只連接匹配的行,以雙方為基準(zhǔn)
#找兩張表共有的部分,相當(dāng)于利用條件從笛卡爾積結(jié)果中篩選出了匹配的結(jié)果 #department沒(méi)有204這個(gè)部門,因而employee表中關(guān)于204這條員工信息沒(méi)有匹配出來(lái) mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id; +----+---------+------+--------+--------------+ | id | name | age | sex | name | +----+---------+------+--------+--------------+ | 1 | egon | 18 | male | 技術(shù) | | 2 | alex | 48 | female | 人力資源 | | 3 | wupeiqi | 38 | male | 人力資源 | | 4 | yuanhao | 28 | female | 銷售 | | 5 | nvshen | 18 | male | 技術(shù) | +----+---------+------+--------+--------------+ 5 rows in set (0.00 sec)
#上述sql等同于 mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
(3)外鏈接之左連接:優(yōu)先顯示左表全部記錄
#以左表為準(zhǔn),即找出所有員工信息,當(dāng)然包括沒(méi)有部門的員工 #本質(zhì)就是:在內(nèi)連接的基礎(chǔ)上增加左邊有,右邊沒(méi)有的結(jié)果 mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id; +----+----------+--------------+ | id | name | depart_name | +----+----------+--------------+ | 1 | egon | 技術(shù) | | 5 | nvshen | 技術(shù) | | 2 | alex | 人力資源 | | 3 | wupeiqi | 人力資源 | | 4 | yuanhao | 銷售 | | 6 | xiaomage | NULL | +----+----------+--------------+ 6 rows in set (0.00 sec)
(4) 外鏈接之右連接:優(yōu)先顯示右表全部記錄
#以右表為準(zhǔn),即找出所有部門信息,包括沒(méi)有員工的部門 #本質(zhì)就是:在內(nèi)連接的基礎(chǔ)上增加右邊有,左邊沒(méi)有的結(jié)果 mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id; +------+---------+--------------+ | id | name | depart_name | +------+---------+--------------+ | 1 | egon | 技術(shù) | | 2 | alex | 人力資源 | | 3 | wupeiqi | 人力資源 | | 4 | yuanhao | 銷售 | | 5 | nvshen | 技術(shù) | | NULL | NULL | 運(yùn)營(yíng) | +------+---------+--------------+ 6 rows in set (0.00 sec)
(5) 全外連接:顯示左右兩個(gè)表全部記錄(了解)
#外連接:在內(nèi)連接的基礎(chǔ)上增加左邊有右邊沒(méi)有的和右邊有左邊沒(méi)有的結(jié)果
#注意:mysql不支持全外連接 full JOIN
#強(qiáng)調(diào):mysql可以使用此種方式間接實(shí)現(xiàn)全外連接語(yǔ)法:select * from employee left join department on employee.dep_id = department.id
union all
select * from employee right join department on employee.dep_id = department.id;
mysql> select * from employee left join department on employee.dep_id = department.id union select * from employee right join department on employee.dep_id = department.id ; +------+----------+--------+------+--------+------+--------------+ | id | name | sex | age | dep_id | id | name | +------+----------+--------+------+--------+------+--------------+ | 1 | egon | male | 18 | 200 | 200 | 技術(shù) | | 5 | nvshen | male | 18 | 200 | 200 | 技術(shù) | | 2 | alex | female | 48 | 201 | 201 | 人力資源 | | 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 | | 4 | yuanhao | female | 28 | 202 | 202 | 銷售 | | 6 | xiaomage | female | 18 | 204 | NULL | NULL | | NULL | NULL | NULL | NULL | NULL | 203 | 運(yùn)營(yíng) | +------+----------+--------+------+--------+------+--------------+ 7 rows in set (0.01 sec) #注意 union與union all的區(qū)別:union會(huì)去掉相同的紀(jì)錄
二、符合條件連接查詢
以內(nèi)連接的方式查詢employee和department表,并且employee表中的age字段值必須大于25,即找出年齡大于25歲的員工以及員工所在的部門
select employee.name,department.name from employee inner join department on employee.dep_id = department.id where age > 25;
三、子查詢
#1:子查詢是將一個(gè)查詢語(yǔ)句嵌套在另一個(gè)查詢語(yǔ)句中。
#2:內(nèi)層查詢語(yǔ)句的查詢結(jié)果,可以為外層查詢語(yǔ)句提供查詢條件。
#3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關(guān)鍵字
#4:還可以包含比較運(yùn)算符:= 、 !=、> 、<等
(1)帶in關(guān)鍵字的子查詢
#查詢平均年齡在25歲以上的部門名 select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25); # 查看技術(shù)部員工姓名 select name from employee where dep_id in (select id from department where name='技術(shù)'); #查看不足1人的部門名 select name from department where id not in (select dep_id from employee group by dep_id);
(2)帶比較運(yùn)算符的子查詢
#比較運(yùn)算符:=、!=、>、>=、<、<=、<> #查詢大于所有人平均年齡的員工名與年齡 mysql> select name,age from employee where age > (select avg(age) from employee); +---------+------+ | name | age | +---------+------+ | alex | 48 | | wupeiqi | 38 | +---------+------+ #查詢大于部門內(nèi)平均年齡的員工名、年齡
思路:
(1)先對(duì)員工表(employee)中的人員分組(group by),查詢出dep_id以及平均年齡。
(2)將查出的結(jié)果作為臨時(shí)表,再對(duì)根據(jù)臨時(shí)表的dep_id和employee的dep_id作為篩選條件將employee表和臨時(shí)表進(jìn)行內(nèi)連接。
(3)最后再將employee員工的年齡是大于平均年齡的員工名字和年齡篩選。
mysql> select t1.name,t1.age from employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age; +------+------+ | name | age | +------+------+ | alex | 48 |
(3)帶EXISTS關(guān)鍵字的子查詢
#EXISTS關(guān)字鍵字表示存在。在使用EXISTS關(guān)鍵字時(shí),內(nèi)層查詢語(yǔ)句不返回查詢的記錄。而是返回一個(gè)真假值。True或False #當(dāng)返回True時(shí),外層查詢語(yǔ)句將進(jìn)行查詢;當(dāng)返回值為False時(shí),外層查詢語(yǔ)句不進(jìn)行查詢 #department表中存在dept_id=203,Ture mysql> select * from employee where exists (select id from department where id=200); +----+----------+--------+------+--------+ | id | name | sex | age | dep_id | +----+----------+--------+------+--------+ | 1 | egon | male | 18 | 200 | | 2 | alex | female | 48 | 201 | | 3 | wupeiqi | male | 38 | 201 | | 4 | yuanhao | female | 28 | 202 | | 5 | nvshen | male | 18 | 200 | | 6 | xiaomage | female | 18 | 204 | +----+----------+--------+------+--------+ #department表中存在dept_id=205,F(xiàn)alse mysql> select * from employee where exists (select id from department where id=204); Empty set (0.00 sec)
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
- IDEA鏈接MySQL報(bào)錯(cuò)08001和連接成功后不顯示表的問(wèn)題及解決方法
- mysql安裝navicat之后,出現(xiàn)2059,Authentication plugin及本地鏈接虛擬機(jī)docker,遠(yuǎn)程鏈接服務(wù)器
- python pymysql鏈接數(shù)據(jù)庫(kù)查詢結(jié)果轉(zhuǎn)為Dataframe實(shí)例
- 實(shí)例操作MySQL短鏈接
- MySql 8.0.11 安裝過(guò)程及 Navicat 鏈接時(shí)遇到的問(wèn)題小結(jié)
- 詳解MySQL分組鏈接的使用技巧
- MySql使用skip-name-resolve解決外網(wǎng)鏈接客戶端過(guò)慢問(wèn)題
- 利用ssh tunnel鏈接mysql服務(wù)器的方法
- MySQL 查看鏈接及殺掉異常鏈接的方法
相關(guān)文章
mysql問(wèn)題之slow log中出現(xiàn)大量的binlog dump記錄的解決方法
今天在查看mysql中發(fā)現(xiàn)比較慢,然后我使用了slow log,發(fā)現(xiàn)出現(xiàn)了大量的binlog dump記錄,下面我來(lái)給大家整理一下這個(gè)問(wèn)題的解決辦法2013-09-09MySQL數(shù)據(jù)庫(kù)表的合并與分區(qū)實(shí)現(xiàn)介紹
今天我們來(lái)聊聊處理大數(shù)據(jù)時(shí)Mysql的存儲(chǔ)優(yōu)化。當(dāng)數(shù)據(jù)達(dá)到一定量時(shí),一般的存儲(chǔ)方式就無(wú)法解決高并發(fā)問(wèn)題了。最直接的MySQL優(yōu)化就是分區(qū)分表,以下是我個(gè)人對(duì)分區(qū)分表的筆記2022-09-09搞定mysql行轉(zhuǎn)列的7種方法以及列轉(zhuǎn)行
在MySQL數(shù)據(jù)庫(kù)中,有時(shí)候我們需要將一列數(shù)據(jù)轉(zhuǎn)化為行數(shù)據(jù),以便更好地進(jìn)行數(shù)據(jù)分析和處理,下面這篇文章主要給大家介紹了關(guān)于搞定mysql行轉(zhuǎn)列的7種方法以及列轉(zhuǎn)行的相關(guān)資料,需要的朋友可以參考下2024-03-03MySQL中CASE?WHEN語(yǔ)句用法、示例與解析舉例
這篇文章主要給大家介紹了關(guān)于MySQL中CASE?WHEN語(yǔ)句用法、示例與解析的相關(guān)資料,case when語(yǔ)句用于計(jì)算條件列表并返回多個(gè)可能結(jié)果表達(dá)式之一,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05詳解使用navicat連接遠(yuǎn)程linux mysql數(shù)據(jù)庫(kù)出現(xiàn)10061未知故障
這篇文章主要介紹了navicat連接遠(yuǎn)程linux mysql數(shù)據(jù)庫(kù)出現(xiàn)10061未知故障,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04分享幾個(gè)簡(jiǎn)單MySQL優(yōu)化小妙招
這篇文章主要介紹了分享幾個(gè)簡(jiǎn)單MySQL優(yōu)化小妙招,分享內(nèi)容有、設(shè)置大小寫不敏感、MySql?的用戶和權(quán)限管理等內(nèi)容,需要的小伙伴可以參考一下,需要的朋友可以參考下2022-03-03