07、多表查詢
阿新 • • 發佈:2022-05-12
一、介紹
-
多表連線查詢之連表查詢
-
符合條件連線查詢
-
多表連線查詢之子查詢
準備表
# 建表
create table dep(
id int primary key auto_increment,
name varchar(20)
);
create table emp(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);
# 插入資料
insert into dep values
(200,'技術'),
(201,'人力資源'),
(202,'銷售'),
(203,'運營'),
(205,'安保');
insert into emp(name,sex,age,dep_id) values
('jason','male',18,200),
('tony','female',48,201),
('kevin','male',18,201),
('nick','male',28,202),
('owen','male',18,203),
('jerry','female',18,204);
二、多表連線查詢之連表查詢
1、語法
select 欄位列表
from 表1 inner|left|right join 表2
on 表1.欄位 = 表2.欄位;
2、交叉連線:不適用任何匹配條件。生成笛卡爾積
mysql> select * from dep,emp;
+-----+--------------+----+-------+--------+------+--------+
| id | name | id | name | sex | age | dep_id |
+-----+--------------+----+-------+--------+------+--------+
| 200 | 技術 | 1 | jason | male | 18 | 200 |
| 201 | 人力資源 | 1 | jason | male | 18 | 200 |
| 202 | 銷售 | 1 | jason | male | 18 | 200 |
| 203 | 運營 | 1 | jason | male | 18 | 200 |
| 200 | 技術 | 2 | tony | female | 48 | 201 |
| 201 | 人力資源 | 2 | tony | female | 48 | 201 |
| 202 | 銷售 | 2 | tony | female | 48 | 201 |
| 203 | 運營 | 2 | tony | female | 48 | 201 |
| 200 | 技術 | 3 | kevin | male | 18 | 201 |
| 201 | 人力資源 | 3 | kevin | male | 18 | 201 |
| 202 | 銷售 | 3 | kevin | male | 18 | 201 |
| 203 | 運營 | 3 | kevin | male | 18 | 201 |
| 200 | 技術 | 4 | nick | male | 28 | 202 |
| 201 | 人力資源 | 4 | nick | male | 28 | 202 |
| 202 | 銷售 | 4 | nick | male | 28 | 202 |
| 203 | 運營 | 4 | nick | male | 28 | 202 |
| 200 | 技術 | 5 | owen | male | 18 | 203 |
| 201 | 人力資源 | 5 | owen | male | 18 | 203 |
| 202 | 銷售 | 5 | owen | male | 18 | 203 |
| 203 | 運營 | 5 | owen | male | 18 | 203 |
| 200 | 技術 | 6 | jerry | female | 18 | 204 |
| 201 | 人力資源 | 6 | jerry | female | 18 | 204 |
| 202 | 銷售 | 6 | jerry | female | 18 | 204 |
| 203 | 運營 | 6 | jerry | female | 18 | 204 |
+-----+--------------+----+-------+--------+------+--------+
mysql> select emp.name,dep.name from emp,dep where emp.dep_id = dep.id;
+-------+--------------+
| name | name |
+-------+--------------+
| jason | 技術 |
| tony | 人力資源 |
| kevin | 人力資源 |
| nick | 銷售 |
| owen | 運營 |
+-------+--------------+
"""
涉及到多表操作的時候 為了避免表字段重複
需要在欄位名的前面加上表名限制
"""
3、內連線:只連線匹配的行
inner join 內連線:只連線兩表中都存在(有對應關係)的資料
mysql> select * from emp inner join dep on emp.dep_id = dep.id;
+----+-------+--------+------+--------+-----+--------------+
| id | name | sex | age | dep_id | id | name |
+----+-------+--------+------+--------+-----+--------------+
| 1 | jason | male | 18 | 200 | 200 | 技術 |
| 2 | tony | female | 48 | 201 | 201 | 人力資源 |
| 3 | kevin | male | 18 | 201 | 201 | 人力資源 |
| 4 | nick | male | 28 | 202 | 202 | 銷售 |
| 5 | owen | male | 18 | 203 | 203 | 運營 |
+----+-------+--------+------+--------+-----+--------------+
# 上述sql等同於
mysql> select * from emp,dep where emp.dep_id = dep.id;
4、左連線:優先顯示左表全部記錄
left join 左連線:以左表為基準展示左表所有的資料沒有對應則NULL填充
mysql> select * from emp left join dep on emp.dep_id = dep.id;
+----+-------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+-------+--------+------+--------+------+--------------+
| 1 | jason | male | 18 | 200 | 200 | 技術 |
| 2 | tony | female | 48 | 201 | 201 | 人力資源 |
| 3 | kevin | male | 18 | 201 | 201 | 人力資源 |
| 4 | nick | male | 28 | 202 | 202 | 銷售 |
| 5 | owen | male | 18 | 203 | 203 | 運營 |
| 6 | jerry | female | 18 | 204 | NULL | NULL |
+----+-------+--------+------+--------+------+--------------+
5、右連線:優先顯示右表全部記錄
right join 右連線:以右表為基準展示右表所有的資料沒有對應則NULL填充
mysql> select * from emp right join dep on emp.dep_id = dep.id;
+------+-------+--------+------+--------+-----+--------------+
| id | name | sex | age | dep_id | id | name |
+------+-------+--------+------+--------+-----+--------------+
| 1 | jason | male | 18 | 200 | 200 | 技術 |
| 2 | tony | female | 48 | 201 | 201 | 人力資源 |
| 3 | kevin | male | 18 | 201 | 201 | 人力資源 |
| 4 | nick | male | 28 | 202 | 202 | 銷售 |
| 5 | owen | male | 18 | 203 | 203 | 運營 |
| NULL | NULL | NULL | NULL | NULL | 205 | 安保 |
+------+-------+--------+------+--------+-----+--------------+
6、全連線:顯示左右兩個表全部記錄
union 全連線:展示左右兩表中所有的資料沒有對應則NULL填充
mysql> select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;
+------+-------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+-------+--------+------+--------+------+--------------+
| 1 | jason | male | 18 | 200 | 200 | 技術 |
| 2 | tony | female | 48 | 201 | 201 | 人力資源 |
| 3 | kevin | male | 18 | 201 | 201 | 人力資源 |
| 4 | nick | male | 28 | 202 | 202 | 銷售 |
| 5 | owen | male | 18 | 203 | 203 | 運營 |
| 6 | jerry | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 205 | 安保 |
+------+-------+--------+------+--------+------+--------------+
三、子查詢
#1:子查詢是將一個查詢語句巢狀在另一個查詢語句中,即為分步查詢
#2:內層查詢語句的查詢結果,可以為外層查詢語句提供查詢條件。
#3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關鍵字
#4:還可以包含比較運算子:= 、 !=、> 、<等
1、帶 IN 關鍵字的子查詢
# 查詢部門是技術或者人力資源的員工資訊 1.先查詢技術和人力資源的部門編號 select id from dep where name in ('技術','人力資源'); 2.根據部門編號去員工表中篩選出對應的員工資料 select * from emp where dep_id in (200,201); '''子查詢:將SQL語句括號括起來即可充當查詢條件''' mysql> select * from emp where dep_id in (select id from dep where name in ('技術','人力資源')); # 查詢平均年齡在25歲以上的部門名 mysql> select name from dep where id in (select dep_id from emp group by dep_id having avg(age)>25); +--------------+ | name | +--------------+ | 人力資源 | | 銷售 | +--------------+ # 檢視技術部員工姓名 mysql> select name from emp where dep_id in (select id from dep where name = '技術'); +-------+ | name | +-------+ | jason | +-------+ # 檢視不足1人的部門名(子查詢得到的是有人的部門id) mysql> select name from dep where id not in (select distinct dep_id from emp); +--------+ | name | +--------+ | 安保 | +--------+ """ not in 無法處理null的值,即子查詢中如果存在null的值,not in將無法處理 """
2、帶ANY關鍵字的子查詢
# 在 SQL 中 ANY 和 SOME 是同義詞,SOME 的用法和功能和 ANY 一模一樣。 # ANY 和 IN 運算子不同之處1 ANY 必須和其他的比較運算子共同使用,而且ANY必須將比較運算子放在 ANY 關鍵字之前,所比較的值需要匹配子查詢中的任意一個值,這也就是 ANY 在英文中所表示的意義 例如:使用 IN 和使用 ANY運算子得到的結果是一致的 select * from employee where salary = any ( select max(salary) from employee group by depart_id); select * from employee where salary in ( select max(salary) from employee group by depart_id); # 結論:也就是說“=ANY”等價於 IN 運算子,而“<>ANY”則等價於 NOT IN 運算子 # ANY和 IN 運算子不同之處2 ANY 運算子不能與固定的集合相匹配,比如下面的 SQL 語句是錯誤的 SELECT * FROM T_Book WHERE FYearPublished < ANY (2001, 2003, 2005)
3、帶ALL關鍵字的子查詢
# all同any類似,只不過all表示的是所有,any表示任一 查詢出那些薪資比所有部門的平均薪資都高的員工=》薪資在所有部門平均線以上的狗幣資本家 select * from employee where salary > all ( select avg(salary) from employee group by depart_id); 查詢出那些薪資比所有部門的平均薪資都低的員工=》薪資在所有部門平均線以下的無產階級勞苦大眾 select * from employee where salary < all ( select avg(salary) from employee group by depart_id); 查詢出那些薪資比任意一個部門的平均薪資低的員工=》薪資在任一部門平均線以下的員工 select * from employee where salary < any ( select avg(salary) from employee group by depart_id);
4、帶比較運算子的子查詢
#比較運算子:=、!=、>、>=、<、<=、<> #查詢大於所有人平均年齡的員工名與年齡 mysql> select * from emp where age > (select avg(age) from emp); +----+------+--------+------+--------+ | id | name | sex | age | dep_id | +----+------+--------+------+--------+ | 2 | tony | female | 48 | 201 | | 4 | nick | male | 28 | 202 | +----+------+--------+------+--------+ #查詢大於部門內平均年齡的員工名、年齡 mysql> 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; +------+------+ | name | age | +------+------+ | tony | 48 | +------+------+
5、帶EXISTS關鍵字的子查詢
# EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄。 # 而是返回一個真假值。True或False # 當返回True時,外層查詢語句將進行查詢;當返回值為False時,外層查詢語句不進行查詢 mysql> select * from emp -> where exists -> (select id from dep where id = 2000); Empty set (0.00 sec) mysql> select * from emp -> where exists -> (select id from dep where id = 200); +----+-------+--------+------+--------+ | id | name | sex | age | dep_id | +----+-------+--------+------+--------+ | 1 | jason | male | 18 | 200 | | 2 | tony | female | 48 | 201 | | 3 | kevin | male | 18 | 201 | | 4 | nick | male | 28 | 202 | | 5 | owen | male | 18 | 203 | | 6 | jerry | female | 18 | 204 | +----+-------+--------+------+--------+
四、符合條件連線查詢
#示例1:以內連線的方式查詢emp和dep表,並且emp表中的age欄位值必須大於25,即找出年齡大於25歲的員工以及員工所在的部門 mysql> select emp.name,dep.name from emp inner join dep on emp.dep_id = dep.id where emp.age>25; +------+--------------+ | name | name | +------+--------------+ | tony | 人力資源 | | nick | 銷售 | +------+--------------+ #示例2:以內連線的方式查詢emp和dep表,並且以age欄位的升序方式顯示 mysql> select * from emp inner join dep on emp.dep_id = dep.id order by age asc; +----+-------+--------+------+--------+-----+--------------+ | id | name | sex | age | dep_id | id | name | +----+-------+--------+------+--------+-----+--------------+ | 1 | jason | male | 18 | 200 | 200 | 技術 | | 3 | kevin | male | 18 | 201 | 201 | 人力資源 | | 5 | owen | male | 18 | 203 | 203 | 運營 | | 4 | nick | male | 28 | 202 | 202 | 銷售 | | 2 | tony | female | 48 | 201 | 201 | 人力資源 | +----+-------+--------+------+--------+-----+--------------+
五、視覺化軟體
為了提高開發的效率 但是不能太過於依賴該軟體 其實這些軟體的底層還是執行的SQL語句來操作資料庫的 該軟體是收費的 但是有很多破解版本 自我百度下載即可(正式版只能使用14天) 1.連結資料庫 2.建立資料庫 3.建立表 4.建立資料 5.建立外來鍵 6.逆向資料庫到模型 7.轉儲SQL檔案