本文實例講述了MySQL多表查詢。分享給大家供大家參考,具體如下:
準備工作:準備兩張表,部門表(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,'技術'),
(201,'人力資源'),
(202,'銷售'),
(203,'運營');
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)
;
# 查看表結構和數(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 | 技術 |
| 201 | 人力資源 |
| 202 | 銷售 |
| 203 | 運營 |
+------+--------------+
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中沒有對應的員工,發(fā)現(xiàn)employee中id=6的員工在department表中沒有對應關系。
一多表鏈接查詢
SELECT 字段列表
FROM 表1 INNER|LEFT|RIGHT JOIN 表2
ON 表1.字段 = 表2.字段;
(1)先看第一種情況交叉連接:不適用任何匹配條件。生成笛卡爾積.--->重復最多
mysql> select * from employee,department;
+----+----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技術 |
| 1 | egon | male | 18 | 200 | 201 | 人力資源 |
| 1 | egon | male | 18 | 200 | 202 | 銷售 |
| 1 | egon | male | 18 | 200 | 203 | 運營 |
| 2 | alex | female | 48 | 201 | 200 | 技術 |
| 2 | alex | female | 48 | 201 | 201 | 人力資源 |
| 2 | alex | female | 48 | 201 | 202 | 銷售 |
| 2 | alex | female | 48 | 201 | 203 | 運營 |
| 3 | wupeiqi | male | 38 | 201 | 200 | 技術 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力資源 |
| 3 | wupeiqi | male | 38 | 201 | 202 | 銷售 |
| 3 | wupeiqi | male | 38 | 201 | 203 | 運營 |
| 4 | yuanhao | female | 28 | 202 | 200 | 技術 |
| 4 | yuanhao | female | 28 | 202 | 201 | 人力資源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 銷售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 運營 |
| 5 | nvshen | male | 18 | 200 | 200 | 技術 |
| 5 | nvshen | male | 18 | 200 | 201 | 人力資源 |
| 5 | nvshen | male | 18 | 200 | 202 | 銷售 |
| 5 | nvshen | male | 18 | 200 | 203 | 運營 |
| 6 | xiaomage | female | 18 | 204 | 200 | 技術 |
| 6 | xiaomage | female | 18 | 204 | 201 | 人力資源 |
| 6 | xiaomage | female | 18 | 204 | 202 | 銷售 |
| 6 | xiaomage | female | 18 | 204 | 203 | 運營 |
(2)內連接:只連接匹配的行,以雙方為基準
#找兩張表共有的部分,相當于利用條件從笛卡爾積結果中篩選出了匹配的結果
#department沒有204這個部門,因而employee表中關于204這條員工信息沒有匹配出來
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 | 技術 |
| 2 | alex | 48 | female | 人力資源 |
| 3 | wupeiqi | 38 | male | 人力資源 |
| 4 | yuanhao | 28 | female | 銷售 |
| 5 | nvshen | 18 | male | 技術 |
+----+---------+------+--------+--------------+
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)先顯示左表全部記錄
#以左表為準,即找出所有員工信息,當然包括沒有部門的員工
#本質就是:在內連接的基礎上增加左邊有,右邊沒有的結果
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 | 技術 |
| 5 | nvshen | 技術 |
| 2 | alex | 人力資源 |
| 3 | wupeiqi | 人力資源 |
| 4 | yuanhao | 銷售 |
| 6 | xiaomage | NULL |
+----+----------+--------------+
6 rows in set (0.00 sec)
(4) 外鏈接之右連接:優(yōu)先顯示右表全部記錄
#以右表為準,即找出所有部門信息,包括沒有員工的部門
#本質就是:在內連接的基礎上增加右邊有,左邊沒有的結果
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 | 技術 |
| 2 | alex | 人力資源 |
| 3 | wupeiqi | 人力資源 |
| 4 | yuanhao | 銷售 |
| 5 | nvshen | 技術 |
| NULL | NULL | 運營 |
+------+---------+--------------+
6 rows in set (0.00 sec)
(5) 全外連接:顯示左右兩個表全部記錄(了解)
#外連接:在內連接的基礎上增加左邊有右邊沒有的和右邊有左邊沒有的結果
#注意:mysql不支持全外連接 full JOIN
#強調:mysql可以使用此種方式間接實現(xiàn)全外連接
語法: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 | 技術 |
| 5 | nvshen | male | 18 | 200 | 200 | 技術 |
| 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 | 運營 |
+------+----------+--------+------+--------+------+--------------+
7 rows in set (0.01 sec)
#注意 union與union all的區(qū)別:union會去掉相同的紀錄
二、符合條件連接查詢
以內連接的方式查詢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:子查詢是將一個查詢語句嵌套在另一個查詢語句中。
#2:內層查詢語句的查詢結果,可以為外層查詢語句提供查詢條件。
#3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關鍵字
#4:還可以包含比較運算符:= 、 !=、> 、等
(1)帶in關鍵字的子查詢
#查詢平均年齡在25歲以上的部門名
select id,name from department
where id in
(select dep_id from employee group by dep_id having avg(age) > 25);
# 查看技術部員工姓名
select name from employee
where dep_id in
(select id from department where name='技術');
#查看不足1人的部門名
select name from department
where id not in
(select dep_id from employee group by dep_id);
(2)帶比較運算符的子查詢
#比較運算符:=、!=、>、>=、、=、>
#查詢大于所有人平均年齡的員工名與年齡
mysql> select name,age from employee where age > (select avg(age) from employee);
+---------+------+
| name | age |
+---------+------+
| alex | 48 |
| wupeiqi | 38 |
+---------+------+
#查詢大于部門內平均年齡的員工名、年齡
思路:
(1)先對員工表(employee)中的人員分組(group by),查詢出dep_id以及平均年齡。
(2)將查出的結果作為臨時表,再對根據(jù)臨時表的dep_id和employee的dep_id作為篩選條件將employee表和臨時表進行內連接。
(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關鍵字的子查詢
#EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄。而是返回一個真假值。True或False
#當返回True時,外層查詢語句將進行查詢;當返回值為False時,外層查詢語句不進行查詢
#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)
更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
您可能感興趣的文章:- IDEA鏈接MySQL報錯08001和連接成功后不顯示表的問題及解決方法
- mysql安裝navicat之后,出現(xiàn)2059,Authentication plugin及本地鏈接虛擬機docker,遠程鏈接服務器
- python pymysql鏈接數(shù)據(jù)庫查詢結果轉為Dataframe實例
- 實例操作MySQL短鏈接
- MySql 8.0.11 安裝過程及 Navicat 鏈接時遇到的問題小結
- 詳解MySQL分組鏈接的使用技巧
- MySql使用skip-name-resolve解決外網(wǎng)鏈接客戶端過慢問題
- 利用ssh tunnel鏈接mysql服務器的方法
- MySQL 查看鏈接及殺掉異常鏈接的方法