在開始之前,我們需要建立表,做建表和資料的準備的工作。
1.建表
create table department(
id int,
name varchar(20)
);
create table employee(
id int primary key auto_increment,
name varchar(20),
sex enmu('male','female') not null default = 'male',
age int,
dep_id int,
constraint fk_id foreign key(dep_id) references department(id),
);
2.插入資料
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),
('liwenzhou','male',18,200),
('jingliyang','female',18,204);
# 檢視錶結構
desc department;
desc employee;
# 檢視我們插入的資料
select * from department;
select * from employee;
多表的查詢,我們主要是分為兩種查詢,這兩種查詢分別是多表連線查詢和子查詢兩種情況。
多表連線查詢
1.交叉連線:不適用任何匹配條件。生成笛卡爾積。
select * from employee,department;
2.內連線
# 找兩張表共有部分,相當於利用笛卡兒積結果中篩選出正確的結果。
# department沒有204這個部門,因而employee表中關於204這條員工資訊沒有匹配出來
select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;
上面的sql語句等價於下面的sql語句
select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
3.外連線之左連線:優先顯示左表全部記錄
# 以左表為標準,即找出所有員工資訊,當然包括沒有部門的員工
# 本質就是:在內連線的基礎上增加左邊有而右邊沒有的結果
select employee.id,employee.name,department.name as depart_name from employee letf join department on employee.dep_id=department.id;
4.外連線之右連線:優先顯示右表全部記錄
# 以右表為標準,即找出所有部門資訊,包括沒有員工的部門
# 本質就是:在內連線的基礎上增加右邊有而左邊沒有的結果
select employee.id,employee.name,department.name as depart_name from employee right join department on empolyee.dep_id=department.id;
5.全外連線:顯示左右兩個表全部的記錄
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;
#注意 union與union all的區別:union會去掉相同的紀錄
6.符合條件連線查詢
# 例項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 inner join department on employee.dep_id=department.id 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);
這兩個sql語句是等價的。
select department.name from employee inner join department on employee.dep_id=department.id where employee.age > (select avg(age) from employee group by dep_id);
#檢視技術部員工姓名
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 |
+---------+------+
| alex | 48 |
| wupeiqi | 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 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | 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)