【MySQL 基础命令】入门三
1.子查询(嵌套查询)
- 查询工资高于1号部门平均工资的员工信息
select avg(sal) from emp where deptno=1;
select * from emp where sal>2325;
- 把上面两条合并成一条
select * from emp where sal>(select avg(sal) from emp where deptno=1);
-
查询拿最高工资的员工信息
select * from emp where sal=(select max(sal) from emp); -
查询工资高于2号部门最低工资的员工信息
select * from emp where sal>(select min(sal) from emp where deptno=2); -
查询和孙悟空相同工作的其他员工信息
select * from emp where job=(select job from emp where ename=‘孙悟空’)
and ename!=‘孙悟空’; -
查询拿最低工资员工的同事们的信息(同事指同一部门的员工)
select min(sal) from emp;
select deptno from emp where sal=(select min(sal) from emp);select * from emp where deptno=(select deptno from emp where sal=(select min(sal) from emp)) and sal!=(select min(sal) from emp);
-
查询白骨精的部门信息(需要用到dept部门表)
select deptno from emp where ename=‘白骨精’;
select * from dept where deptno=(select deptno from emp where ename=‘白骨精’); -
查询所有员工的部门信息(部门只有1,2,3但是部门表里面有1,2,3,4 只根据员工表中出现的部门编号去查询部门信息)
- 先查询员工表中出现的部门编号
select distinct deptno from emp; - 通过查询到的部门编号查询部门信息
select * from dept where deptno in(select distinct deptno from emp);
- 先查询员工表中出现的部门编号
select avg(sal) from emp where deptno=1;
select * from emp where sal>2325;
select * from emp where sal>(select avg(sal) from emp where deptno=1);
select deptno from emp where ename='白骨精';
select * from dept where deptno=(select deptno from emp where ename='白骨精');
概念:SQL语句中嵌套SELECT语句,称为嵌套查询,又称子查询。
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
-- 分页查询条件
SELECT dept_id,max(updated_at) FROM mo_config_detail_people GROUP BY dept_id,updated_at ORDER BY updated_at DESC;
-- 原来的方式
SELECT dept_id ,max(updated_at) FROM mo_config_detail_people WHERE (mo_config_detail_id = 51) GROUP BY dept_id ,updated_at ORDER BY updated_at DESC;
-- 有人更新的时候会出现多条
11268 1692339294
5364 1692338751
11268 1692338686
-- 子查询(部门人员分页关键SQL)
SELECT dept_id,
(SELECT MAX(mo.updated_at)
FROM mo_config_detail_people as mo
WHERE mo.dept_id = mo_config_detail_people.dept_id) AS latest_update
FROM mo_config_detail_people WHERE mo_config_detail_id = 51
GROUP BY dept_id ORDER BY latest_update DESC;
2.关联关系
- 创建表时,表与表之间存在的业务关系称为关联关系
- 外键: 用于建立关系的字段称为外键
- 有哪些关系:
- 一对一:有AB两张表,A表中1条数据对应B表中的1条数据,同时B表中1条数据也对应A表中的一条数据.称为一对一关系
- 应用场景: 将原属于一张表的数据拆分成两张表进行数据保存时,使用1对1的关系.
- 如何建立一对一的关系? (将一张表拆成两张表(主表和从表))
在从表中添加外键指向主表的主键
- 一对多:有AB两张表,A表中1条数据对应B表中的多条数据,同时B表中1条数据对应A表中的一条数据.称为一对多关系
- 应用场景: 比如员工表和部门表的关系
- 如何建立关系?
一对多关系中存在两张表,一个表是1(部门表),一个表是多(员工表),在多的表中添加外键指向另外一张表的主键
- 多对多:有AB两张表,A表中1条数据对应B表中的多条数据,同时B表中1条数据也对应A表中的多条数据.称为多对多关系
- 应用场景: 比如老师表和学生表
- 如何建立关系?
需要通过一个中间关系表建立关系,在关系表中添加两个外键分别指向两个主表的主键
3.关联查询
- 关联查询是查询存在关联关系多张表的查询方式
- 三种关联查询的方式: 1. 等值连接 2.内连接 3.外连接
- 关联查询必须写关联关系,如果不写会得到两个表结果的乘积,这个乘积称为笛卡尔积,笛卡尔积是一个错误的查询结果,由于工作中数据量巨大笛卡尔积有可能会导致内存溢出.
- 等值连接
- 格式: select 字段信息 from A,B where 关联关系 and 其它条件
- 举例:
- 查询工资高于2000的员工的姓名,工资和对应的部门名
select e.ename,e.sal,d.dname
from emp e,dept d
where e.deptno=d.deptno
and e.sal>2000; - 查询不是程序员的员工姓名,工作和部门所在地
select e.ename,e.job,d.loc
from emp e,dept d
where e.deptno=d.deptno
and e.job!=‘程序员’;
- 查询工资高于2000的员工的姓名,工资和对应的部门名
- 内连接
- 格式: select 字段信息 from A join B on 关联关系 where 其它条件
- 举例:
- 查询工资高于2000的员工的姓名,工资和对应的部门名
select e.ename,e.sal,d.dname
from emp e join dept d
on e.deptno=d.deptno
where e.sal>2000; - 查询不是程序员的员工姓名,工作和部门所在地
select e.ename,e.job,d.loc
from emp e join dept d
on e.deptno=d.deptno
where e.job!=‘程序员’;
- 查询工资高于2000的员工的姓名,工资和对应的部门名
- 等值连接和内连接查询到的都是两张表的交集数据,但是内连接代码结构更直观推荐使用
- 外连接
- 外连接查询的是一张表的全部和另外一张表的交集数据
- 格式: select 字段信息 from A left/right join B on 关联关系 where 其它条件
- 举例:
- 查询所有部门名和对应的员工姓名
select d.dname,e.ename
from emp e join dept d
on e.deptno=d.deptno; - 查询所有员工的姓名和部门所在地
- 由于员工表里面全部是交集数据所以先插入一条数据
insert into emp(empno,ename) values(20,‘灭霸’);
- 查询所有部门名和对应的员工姓名
select e.ename,d.dname from emp e left join dept d on e.deptno=d.deptno;