MySQL多表查詢 建表與數據準備 多表連接查詢 1 交叉連接:不適用任何匹配條件。生成笛卡爾積 2 內連接:只連接匹配的行 3 外鏈接之左連接:優先顯示左表全部記錄 4 外鏈接之右連接:優先顯示右表全部記錄 5 全外連接:顯示左右兩個表全部記錄 符合條件連接查詢 子查詢 2 帶比較運算符的子查詢 ...
MySQL多表查詢
建表與數據準備
#建表
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
);
#插入數據
insert into department values
(200,'技術'),
(201,'人力資源'),
(202,'銷售'),
(203,'運營');
insert into employee(name,sex,age,dep_id) values
('daxia','female',18,200),
('anwen','male',23,201),
('wudi','male',38,201),
('yage','female',23,202),
('dafei','male',18,200),
('jingyu','female',18,204)
;
#查看表結構和數據
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
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 | |
+--------+-----------------------+------+-----+---------+----------------+
mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技術 |
| 201 | 人力資源 |
| 202 | 銷售 |
| 203 | 運營 |
+------+--------------+
mysql> select * from employee;
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | daxia | female | 18 | 200 |
| 2 | anwen | male | 23 | 201 |
| 3 | wudi | male | 38 | 201 |
| 4 | yage | female | 23 | 202 |
| 5 | dafei | male | 18 | 200 |
| 6 | jingyu | female | 18 | 204 |
+----+------------+--------+------+--------+
多表連接查詢
#重點:外鏈接語法
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 | daxia | female | 18 | 200 | 200 | 技術 |
| 1 | daxia | female | 18 | 200 | 201 | 人力資源 |
| 1 | daxia | female | 18 | 200 | 202 | 銷售 |
| 1 | daxia | female | 18 | 200 | 203 | 運營 |
| 2 | anwen | male | 48 | 201 | 200 | 技術 |
| 2 | anwen | male | 48 | 201 | 201 | 人力資源 |
| 2 | anwen | male | 48 | 201 | 202 | 銷售 |
| 2 | anwen | male | 48 | 201 | 203 | 運營 |
| 3 | wudi | male | 38 | 201 | 200 | 技術 |
| 3 | wudi | male | 38 | 201 | 201 | 人力資源 |
| 3 | wudi | male | 38 | 201 | 202 | 銷售 |
| 3 | wudi | male | 38 | 201 | 203 | 運營 |
| 4 | yage | female | 28 | 202 | 200 | 技術 |
| 4 | yage | female | 28 | 202 | 201 | 人力資源 |
| 4 | yage | female | 28 | 202 | 202 | 銷售 |
| 4 | yage | female | 28 | 202 | 203 | 運營 |
| 5 | dafei | male | 18 | 200 | 200 | 技術 |
| 5 | dafei | male | 18 | 200 | 201 | 人力資源 |
| 5 | dafei | male | 18 | 200 | 202 | 銷售 |
| 5 | dafei | male | 18 | 200 | 203 | 運營 |
| 6 | jingyu | female | 18 | 204 | 200 | 技術 |
| 6 | jingyu | female | 18 | 204 | 201 | 人力資源 |
| 6 | jingyu | female | 18 | 204 | 202 | 銷售 |
| 6 | jingyu | 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 | daxia | 18 | female | 技術 |
| 2 | anwen | 48 | male | 人力資源 |
| 3 | wudi | 38 | male | 人力資源 |
| 4 | yage | 28 | female | 銷售 |
| 5 | dafei | 18 | male | 技術 |
+----+-----------+------+--------+--------------+
#上述sql等同於
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
3 外鏈接之左連接:優先顯示左表全部記錄
#以左表為準,即找出所有員工信息,當然包括沒有部門的員工
#本質就是:在內連接的基礎上增加左邊有右邊沒有的結果
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 | daxia | 技術 |
| 5 | dafei | 技術 |
| 2 | anwen | 人力資源 |
| 3 | wudi | 人力資源 |
| 4 | dafei | 銷售 |
| 6 | jingyu | NULL |
+----+------------+--------------+
4 外鏈接之右連接:優先顯示右表全部記錄
#以右表為準,即找出所有部門信息,包括沒有員工的部門
#本質就是:在內連接的基礎上增加右邊有左邊沒有的結果
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 | daxia | 技術 |
| 2 | anwen | 人力資源 |
| 3 | wudi | 人力資源 |
| 4 | yage | 銷售 |
| 5 | dafei | 技術 |
| NULL | NULL | 運營 |
+------+-----------+--------------+
5 全外連接:顯示左右兩個表全部記錄
全外連接:在內連接的基礎上增加左邊有右邊沒有的和右邊有左邊沒有的結果
#註意:mysql不支持全外連接 full JOIN
#強調: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 | daxia | female | 18 | 200 | 200 | 技術 |
| 5 | dafei | male | 18 | 200 | 200 | 技術 |
| 2 | anwen | male | 48 | 201 | 201 | 人力資源 |
| 3 | wudi | male | 38 | 201 | 201 | 人力資源 |
| 4 | yage | female | 28 | 202 | 202 | 銷售 |
| 6 | jingyu | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 運營 |
+------+------------+--------+------+--------+------+--------------+
#註意 union與union all的區別:union會去掉相同的紀錄
符合條件連接查詢
#示例1:以內連接的方式查詢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;
#示例2:以內連接的方式查詢employee和department表,並且以age欄位的升序方式顯示
select employee.id,employee.name,employee.age,department.name from employee,department
where employee.dep_id = department.id
and age > 25
order by age asc;
子查詢
#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人的部門名(子查詢得到的是有人的部門id)
select name from department where id not in (select distinct dep_id from employee);
2 帶比較運算符的子查詢
#比較運算符:=、!=、>、>=、<、<=、<>
#查詢大於所有人平均年齡的員工名與年齡
mysql> select name,age from emp where age > (select avg(age) from emp);
+---------+------+
| name | age |
+---------+------+
| anwen | 48 |
| wudi | 38 |
+---------+------+
2 rows in set (0.00 sec)
#查詢大於部門內平均年齡的員工名、年齡
select t1.name,t1.age from emp t1
inner join
(select dep_id,avg(age) avg_age from emp group by dep_id) t2
on t1.dep_id = t2.dep_id
where t1.age > t2.avg_age;
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 | daxia | female | 18 | 200 |
| 2 | anwen | male | 48 | 201 |
| 3 | wudi | male | 38 | 201 |
| 4 | yage | female | 28 | 202 |
| 5 | dafei | male | 18 | 200 |
| 6 | jingyu | female | 18 | 204 |
+----+------------+--------+------+--------+
#department表中存在dept_id=205,False
mysql> select * from employee
-> where exists
-> (select id from department where id=204);
Empty set (0.00 sec)