多表查询
多表关系
概述
项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着各种联系,基本上分为三种:
- 一对多(多对一)
- 多对多
- 一对一
一对多(多对一)
- 案例:部门与员工的关系
- 关系:一个部门对应多个员工,一个员工对应一个部门
- 实现:在多的预防建立外键,指向一的一方的主键
多对多
- 案例:学生与课程的关系
- 关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择
案例:将课程表和学生表联系起来,变成一个中间表
create table student_course(
id int auto_increment comment '主键' primary key,
studentid int not null commment '学生ID',
courseid int not null comment '课程ID',
constraint fk_courseid foreign key (courseid) references couse (id),
constraint fk_courseid foreign key (studentid) references student (id)
)commment '学生课程中间表';
一对一
- 用户与用户详情的关系
- 关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率。
- 实现:在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(unique)。
create table tb_user(
id int auto_increment primary key commment '主键ID',
name varchar(10) comment '姓名',
age int comment '年龄',
gender char(1) comment '1:男 , 2: 女',
phone char(11) comment '手机号',
)comment '用户基本信息表';
create table tb_user_edu(
id int auto_increment primary key comment '主键ID',
degree varchar(20) comment '学历',
major varchar(50) comment '专业',
primaryschool varchar(50) comment '小学',
middleshool varchar(50) comment '初中',
university varchar(50) comment '大学',
userid int unique comment '用户ID',
constraint fk_userid foreign key (userid) references tb_user(id)
)comment '用户教育信息表';
多表查询概述
概述
指从多张表中查询数据
笛卡尔积
笛卡尔乘积是指在数学中,两个集合A集合和B集合的所有组合情况。(在多表查询时,需要消除无效的笛卡尔积)
eg:select * from emp,dept where emp.dept_id = dept.id;
多表查询分类
连接查询
内连接
相当于查询A、B交集部分数据。
外连接
- 左外连接:查询左表所有数据,以及两张表交集部分数据。
- 右外连接:查询右表所有数据,以及两张表交集部分数据。
自连接
当前表与自身的连接查询,自连接必须使用表别名。
子查询
内连接
内连接查询语法
隐式内连接
select 字段列表 from 表1, 表2 where 条件...;
显式内连接
select 字段列表 from 表1 [inner] join 表2 on 连接条件...; (inner 可省略也可不省略)
内连接查询的是两张表交集的部分
案例:查询每一个员工的姓名,及关联的部门的名称
表结构:emp,dept
连接条件:emp.dept_id=dept.id;
隐式内连接:
select emp.name,dept.name from emp,dept where emp.dept_id=dept.id;
select e.name,d.name from emp e,dept d where e.dept_id=d.id;
显式内连接:
select emp.name,dept.name from emp join dept on emp.dept_id=dept.id;
外连接
外连接查询语法
左外连接
select 字段列表 from 表1 left [outer] on 表2 on 条件...;
相当于查询表1(左表)的所有数据包含表1和表2交集部分的数据。
右外连接
select 字段列表 from 表1 right [outer] on 表2 on 条件...;
相当于查询表1(左表)的所有数据包含表1和表2交集部分的数据。
自连接
自连接查询语法
select 字段列表 from 表A 别名A join 表A 别名B on 条件...;
自连接查询,可以是内连接查询,也可以是外连接查询。
联合查询-union,union all
对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集。
select 字段列表 from 表A...
union [all]
select 字段列表 from 表B...;
eg:将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来
select * from emp where age >50
union
select * from where salary < 5000;
注意:
- 对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致。
- union all 会将全部的数据直接合并在一起,union会对合并之后的数据去重。
子查询
概念
SQL语句中嵌套select语句,成为嵌套语句,成为嵌套查询,又称子查询。
select * from t1 where column1=(select column1 from t2);
子查询外部的语句可以是insert/update/delete/select的任何一个。
子查询分类
- 根据子查询结果不同,分为:
- 标量子查询(子查询结果为单个值)
- 列子查询(子查询结果为一列)
- 行子查询(子查询的结果为一行)
- 表子查询(子查询的结果为多行多列)
- 根据子查询位置,分为:where之后、from之后、select之后。
标量子查询
- 子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式,这种子查询称为标量子查询。
- 常用的操作符:= <> > >= < <=
案例:查询“销售部”的所有员工信息
a.查询“销售部”的部门ID
b.根据销售部ID,查询员工信息
select *
from emp
where dept_id=(
select id
from dept
where name = '销售部'
);
列子查询
- 子查询返回的结果是一列(可以是多行),这种子查询称为列子查询。
- 常用的操作符:in、not in、any、some、all
| 操作符 | 描述 |
|---|---|
| in | 在指定的集合范围之内,多选一 |
| not in | 不在指定的集合范围之内 |
| any | 子查询返回列表中,有任意一个满足即可 |
| some | 与any等同,使用some的地方都可以使用any |
| all | 子查询返回列表的所有值都必须满足 |
案例:查询“销售部”和“市场部”的所有员工信息
select * from emp where dept_id in(
select id from dept
where name ='销售部' or name = '市场部'
);
案例:查询比财务部所有人工资高的员工
select * from emp where salary > all(
select salary from emp
where dept_id =(
select id from dept
where name = '财务部'
))
案例:查询比研发部任意人工资高的员工
select * from emp where salary > any(
select salary from emp
where dept_id =(
select id from dept
where name = '研发部'
));
行子查询
- 行子查询返回的结果是一行(可以是多列),这种子查询称为行子查询。
- 常用的操作符:= 、<>、not in、in
案例:查询与“张无忌”的薪资及直属领导相同的员工信息;
select * from emp where (salare,managerid) = (
select salare,managerid from emp
where name = '张无忌'
);
表子查询
- 子查询返回的结果是多行多列,这种子查询称为表子查询。
- 常用的操作符:in
案例:查询与“鹿杖客”、“宋远桥”的职位和薪资相同的员工信息。
select * from emp where (job,salare) in (
select job,salare from emp where name = '鹿杖客' or name = '宋远桥'
);
查询入职时间是“2006-01-01”之后的员工信息及其部门信息
select e.*,d.* from (select * from emp where entrydate > '2006-01-01')e
left join dept d on e.dept_id = d.id;
多表查询案例
根据需求,完成SQL语句的编写
-
查询员工的姓名、年龄、职位、部门信息。
select name,age,job,dept.name from emp,dept where dept.id=emp.dept_id; -
查询年龄小于30岁的员工姓名、年龄、职位、部门信息。
select e.name,e.age,e.job,d.name from emp e join dept d on e.dept_id=d.id where age < 30; -
查询拥有员工的部门ID、部门名称。
select distinct d.id,d.name from emp e , dept d where e.dept_id=d.id; -
查所有年龄大40岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要展示出来。
select emp.*,dept.name from emp left join dept on emp.dept_id=dept.id where age > 40; -
查询所有员工的工资等级
select e.*,s.grade,s.losal,s.hisal from emp e,salgrade s where e.salary >= s.losal and e.salary <= s.hisal;select e.*,s.grade,s.losal,s.hisal from emp e,salgrade s where e.salary between s.losal and s.hisal; -
查询"研发部"所有员工的信息及工资等级。
select e.*,s.grade from emp e,dept d,salgrade s where e.dept_id=d.id and (e.salary between s.losal and s.hisal) and d.name = '研发部'; -
查询"研发部"员工的平均工资。
select avg(e.salary) from emp e,dept d where e.dept_id = d.id and d.name = '研发部'; -
查询工资比"灭绝"高的员工信息。
select * from emp where salary > ( select salary from emp where name = '灭绝'); -
查询比平均薪资高的员工信息。
select * from emp where salary > (select avg(salary) from emp); -
查询低于本部门平均工资的员工信息。
select * from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id); -
查询所有的部门信息,并统计部门的员工人数。
select d.id,d.name,(select count(*) from emp e where e.dept_id = d.id)'人数' from dept d; -
查询所有学生的选课情况,展示出学生名称,学号,课程名称
表:student,course,student_course
select s.name,s.no,c.name from student s, student_course sc , course c where s.id = sc.studentid and sc.courseid = c.id;