数据库之mysql多表查询(连表)以及pymysql等相关内容-45
#建表
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
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('liwenzhou','male',18,200),
('jingliyang','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 | 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与employee
多表联合查询
# 方案1:链表
把多张物理表合并成一张虚拟表,再进行后续查询
#======>内链接:保留两张表有对应关系的记录
select * from emp,dep where emp.dep_id=dep.id;
select dep.name,emp.name from emp inner join dep on emp.dep_id=dep.id
where dep.name = "技术";
#======>左链接:在内链接的基础上保留左表的记录
select * from emp left join dep on emp.dep_id=dep.id;
#======>右链接:在内链接的基础上保留右表的记录
select * from emp right join dep on emp.dep_id=dep.id;
#======>全外链接:在内链接的基础上保留左右表的记录
full join
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;
示例1:查询所有部门名及对应的员工个数
select dep.name,count(emp.id) from emp right join dep on emp.dep_id = dep.id
group by dep.name
;
select dep.name,count(emp.id) from emp right join dep on emp.dep_id = dep.id
group by dep.name
having count(emp.id) < 2
;
#示例2:即找出年龄大于25岁的员工以及员工所在的部门
select emp.name,dep.name from emp inner join dep on emp.dep_id = dep.id where age > 25;
#示例3:以内连接的方式查询employee和department表,并且以age字段的升序方式显示
# 把多张表链接到一起:
select * from
(select emp.*,dep.name as dep_name from emp inner join dep on emp.dep_id = dep.id) as t1
inner join
dep
on t1.dep_id = dep.id
;
select * from emp
inner join dep
on emp.dep_id = dep.id
inner join dep as t1
on t1.id = dep.id;
# 查询部门内最新入职的员工
select * from employee
inner join
(select depart_id,max(hire_date) as maxd from employee group by depart_id) as t1
on employee.depart_id = t1.depart_id
where employee.hire_date = t1.maxd
;
# 方案2:子查询
从一张表中查询出结果,用该结果作为查
询下一张表的过滤条件
select * from employee
where hire_date = (select max(hire_date) from employee);