基本查询回顾
- 查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J
select * from EMP where (sal>500 or job='MANAGER') and ename like 'J%';
![![[Pasted image 20260508184401.png]]](https://i-blog.csdnimg.cn/direct/f5e1dbb6ee52474388044f07ac963e05.png)
- 按照部门号升序而雇员的工资降序排序
select * from EMP order by deptno, sal desc;
![![[Pasted image 20260508184514.png]]](https://i-blog.csdnimg.cn/direct/cd11b21582a44ff083add71a522503d8.png)
![![[Pasted image 20260508184812.png]]](https://i-blog.csdnimg.cn/direct/7b1b6162561b43f4a9bd6163d30526b0.png)
- 使用年薪进行降序排序
select ename, sal*12+ifnull(comm,0) as '年薪' from EMP order by 年薪 desc;
![![[Pasted image 20260508184833.png]]](https://i-blog.csdnimg.cn/direct/5848636304b14d9bb1ad5cdc3b07cff3.png)
![![[Pasted image 20260508184902.png]]](https://i-blog.csdnimg.cn/direct/76c25dc347594b8fb1b001977f4e512c.png)
![![[Pasted image 20260508184914.png]]](https://i-blog.csdnimg.cn/direct/61191c7aef6344c0887b27f7325ec8aa.png)
![![[Pasted image 20260508184932.png]]](https://i-blog.csdnimg.cn/direct/7455deb2ef1a460abf8aa3c99b56a925.png)
- 显示工资最高的员工的名字和工作岗位
select ename, job from EMP where sal = (select max(sal) from EMP);
![![[Pasted image 20260508185304.png]]](https://i-blog.csdnimg.cn/direct/28b3f56a31eb4c4db147ad3002421741.png)
- 显示工资高于平均工资的员工信息
select ename, sal from EMP where sal>(select avg(sal) from EMP);
![![[Pasted image 20260508185340.png]]](https://i-blog.csdnimg.cn/direct/c50c29a558c64de49be96cbb976a0cbb.png)
- 显示每个部门的平均工资和最高工资
select deptno, format(avg(sal), 2) , max(sal) from EMP group by deptno;
![![[Pasted image 20260508185425.png]]](https://i-blog.csdnimg.cn/direct/784f87e5e7eb48eab9289af0fcdc6909.png)
![![[Pasted image 20260508185504.png]]](https://i-blog.csdnimg.cn/direct/6d0c6a79a5d341dd9ecb08409f9ed37a.png)
- 显示平均工资低于2000的部门号和它的平均工资
select deptno, avg(sal) as avg_sal from EMP group by deptno having avg_sal<2000;
![![[Pasted image 20260508185602.png]]](https://i-blog.csdnimg.cn/direct/a3bbcfa0c2e3490d8f7b78e0a7ce9f0a.png)
- 显示每种岗位的雇员总数,平均工资
select job,count(*), format(avg(sal),2) from EMP group by job;
![![[Pasted image 20260508185655.png]]](https://i-blog.csdnimg.cn/direct/e1b96e604d5b420fbb9df76fc11ae5b6.png)
![![[Pasted image 20260508185705.png]]](https://i-blog.csdnimg.cn/direct/cdf9f9396416415984083ab2b728af90.png)
![![[Pasted image 20260508185727.png]]](https://i-blog.csdnimg.cn/direct/db9b585286fe4d37ae1b12655670bc32.png)
多表查询
实际开发中往往数据来自不同的表,所以需要多表查询。本节我们用一个简单的公司管理系统,有三张表EMP,DEPT,SALGRADE来演示如何进行多表查询
- 显示雇员名、雇员工资以及所在部门的名字因为上面的数据来自EMP和DEPT表,因此要联合查询
![![[Pasted image 20260508185909.png]]](https://i-blog.csdnimg.cn/direct/ceda0d2cbf5643159c091a2cb46dd0ad.png)
其实我们只要emp表中的deptno = dept表中的deptno字段的记录
![![[Pasted image 20260508190159.png]]](https://i-blog.csdnimg.cn/direct/bf99237f11de4b47a15afaf4e52dd672.png)
![![[Pasted image 20260508190209.png]]](https://i-blog.csdnimg.cn/direct/ecb2791e738644a0983d5cb37164f910.png)
![![[Pasted image 20260508190225.png]]](https://i-blog.csdnimg.cn/direct/a38e10ac95bc4023ba48352bae09512a.png)
select EMP.ename, EMP.sal, DEPT.dname from EMP, DEPT where EMP.deptno = DEPT.deptno;
- 显示部门号为10的部门名,员工名和工资
select ename, sal,dname from EMP, DEPT where EMP.deptno=DEPT.deptno and DEPT.deptno = 10;
![![[Pasted image 20260508190334.png]]](https://i-blog.csdnimg.cn/direct/7550ab6fe3f848df80c0a4920d85ac70.png)
![![[Pasted image 20260508190350.png]]](https://i-blog.csdnimg.cn/direct/d97b932308ad4394867fddf7d4aa11c5.png)
- 显示各个员工的姓名,工资,及工资级别
select ename, sal, grade from EMP, SALGRADE where EMP.sal between losal and hisal;
![![[Pasted image 20260508190419.png]]](https://i-blog.csdnimg.cn/direct/80b3ccb4e46e4394a88aeb28b3c7d781.png)
![![[Pasted image 20260508190428.png]]](https://i-blog.csdnimg.cn/direct/af617f6936b14d8f83784fb9616545d7.png)
![![[Pasted image 20260508190438.png]]](https://i-blog.csdnimg.cn/direct/1a0eb6d5359d42529e22a89ee1a9146d.png)
自连接
自连接是指在同一张表连接查询
![![[Pasted image 20260508190510.png]]](https://i-blog.csdnimg.cn/direct/0b5bb134b4bb44e0adc1dc08dd2c1686.png)
显示员工FORD的上级领导的编号和姓名(mgr是员工领导的编号–empno)
- 使用的子查询
select empno,ename from emp where emp.empno=(select mgr from emp where ename='FORD');
![![[Pasted image 20260508190700.png]]](https://i-blog.csdnimg.cn/direct/32a55e5f20294ce5afe7b8074e63df05.png)
![![[Pasted image 20260508190629.png]]](https://i-blog.csdnimg.cn/direct/d81caf2c4ace40c69ba946d3dfaa4afb.png)
![![[Pasted image 20260508190640.png]]](https://i-blog.csdnimg.cn/direct/01a6d0120f774aba9f6421eba6321bda.png)
- 使用多表查询(自查询)
-- 使用到表的别名
--from emp leader, emp worker,给自己的表起别名,因为要先做笛卡尔积,所以别名可以先识别
select leader.empno,leader.ename from emp leader, emp worker where leader.empno = worker.mgr and worker.ename='FORD';
![![[Pasted image 20260508190716.png]]](https://i-blog.csdnimg.cn/direct/024f93775aaa4a6fb05ee7b9c8e5bc36.png)
![![[Pasted image 20260508190726.png]]](https://i-blog.csdnimg.cn/direct/5765924c362347c5965428e9c38d0f3f.png)
子查询
子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询
单行子查询
返回一行记录的子查询
- 显示SMITH同一部门的员工
select * from EMP WHERE deptno = (select deptno from EMP where ename='smith');
![![[Pasted image 20260508190853.png]]](https://i-blog.csdnimg.cn/direct/0b982a6aba9f4cad9fe879e53b11ca97.png)
多行子查询
返回多行记录的子查询
- in关键字;查询和10号部门的工作岗位相同的雇员的名字,岗位,工资,部门号,但是不包含10自己的
select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10) and deptno<>10;
![![[Pasted image 20260508190938.png]]](https://i-blog.csdnimg.cn/direct/b3375543bfe74a8aa375821dd8142e2d.png)
![![[Pasted image 20260508191041.png]]](https://i-blog.csdnimg.cn/direct/5ebb6226766541cf84515efb735b3667.png)
![![[Pasted image 20260508191056.png]]](https://i-blog.csdnimg.cn/direct/d1e4e332947f48bba52ad2f50ffa29e9.png)
![![[Pasted image 20260508191114.png]]](https://i-blog.csdnimg.cn/direct/503b89f10fcd44cfba186e5ef191ff33.png)
- all关键字;显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号
select ename, sal, deptno from EMP where sal > all(select sal from EMP where deptno=30);
![![[Pasted image 20260508191131.png]]](https://i-blog.csdnimg.cn/direct/a99d9ccd50f14e96897ca4d58994f811.png)
![![[Pasted image 20260508191222.png]]](https://i-blog.csdnimg.cn/direct/4d33724b091f40f6bc54c0b113987736.png)
![![[Pasted image 20260508191233.png]]](https://i-blog.csdnimg.cn/direct/e2647482c5a141828ed12a95805387e3.png)
- any关键字;显示工资比部门30的任意员工的工资高的员工的姓名、工资和部门号(包含自己部门的员工)
select ename, sal, deptno from EMP where sal > any(select sal from EMP where deptno=30);
![![[Pasted image 20260508191259.png]]](https://i-blog.csdnimg.cn/direct/379d75e434904b1292a9c71e7445293a.png)
![![[Pasted image 20260508191307.png]]](https://i-blog.csdnimg.cn/direct/74b55017850949d9ac4f2d377f68ed7e.png)
多列子查询
单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言的,而多列子查询则是指查询返回多个列数据的子查询语句
- 查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
select ename from EMP where (deptno, job)=(select deptno, job from EMP where ename='SMITH') and ename <> 'SMITH';
![![[Pasted image 20260508191449.png]]](https://i-blog.csdnimg.cn/direct/9400a7f685c24b2382f72b40378ad3e7.png)
![![[Pasted image 20260508191503.png]]](https://i-blog.csdnimg.cn/direct/f4710208392047f68b9603db703581bf.png)
在from子句中使用子查询
子查询语句出现在from子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用
- 显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资
//获取各个部门的平均工资,将其看作临时表
select ename, deptno, sal, format(asal,2) from EMP, (select avg(sal) asal, deptno dt from EMP group by deptno) tmp where EMP.sal > tmp.asal and EMP.deptno=tmp.dt;
![![[Pasted image 20260508191544.png]]](https://i-blog.csdnimg.cn/direct/e8a08c20fb3f4714992f49d794f32f05.png)
![![[Pasted image 20260508191617.png]]](https://i-blog.csdnimg.cn/direct/3ee94363bd664022be06cd40e2a15d97.png)
![![[Pasted image 20260508191629.png]]](https://i-blog.csdnimg.cn/direct/4dc8fb20d4f74a07848f012310347ad9.png)
![![[Pasted image 20260508191723.png]]](https://i-blog.csdnimg.cn/direct/459c5f8e54cc4b14b1e362a8c8e13bea.png)
![![[Pasted image 20260508191736.png]]](https://i-blog.csdnimg.cn/direct/873db31174fc47f7a3d2042435cde054.png)
![![[Pasted image 20260508191751.png]]](https://i-blog.csdnimg.cn/direct/f85a44c03d5f4e32a9e16760a191981b.png)
- 查找每个部门工资最高的人的姓名、工资、部门、最高工资
select EMP.ename, EMP.sal, EMP.deptno, ms from EMP, (select max(sal) ms, deptno from EMP group by deptno) tmp where EMP.deptno=tmp.deptno and EMP.sal=tmp.ms;
![![[Pasted image 20260508191904.png]]](https://i-blog.csdnimg.cn/direct/bf3be92c9705489cb6cb7599b9dab51c.png)
![![[Pasted image 20260508191925.png]]](https://i-blog.csdnimg.cn/direct/aa8b6205dc9e43aaaf74b36ce3878dcd.png)
![![[Pasted image 20260508191936.png]]](https://i-blog.csdnimg.cn/direct/b82ad954b3f14cecb2dbba533c3590f4.png)
![![[Pasted image 20260508191957.png]]](https://i-blog.csdnimg.cn/direct/bedaf09f33af4637a8e15a104df049cc.png)
- 显示每个部门的信息(部门名,编号,地址)和人员数量
- 使用多表
select DEPT.dname, DEPT.deptno, DEPT.loc,count(*) '部门人数' from EMP,
DEPT where EMP.deptno=DEPT.deptno group by DEPT.deptno,DEPT.dname,DEPT.loc;
![![[Pasted image 20260508192328.png]]](https://i-blog.csdnimg.cn/direct/b68d8399f7234a76b4f1fbe6172b2e8a.png)
![![[Pasted image 20260508192340.png]]](https://i-blog.csdnimg.cn/direct/ef12386473f8437181b3c67eed4d98f3.png)
解决多表问题的本质:想办法将多表转化成为单表,所以 mysql 中,所有 select 的问题全部都可以转成单表问题!
→ 多表查询的指导思想!
- 使用子查询
-- 1. 对EMP表进行人员统计
select count(*), deptno from EMP group by deptno;
-- 2. 将上面的表看作临时表
select DEPT.deptno, dname, mycnt, loc from DEPT, (select count(*) mycnt, deptno from|EMP group by deptno) tmp where DEPT.deptno=tmp.deptno;
![![[Pasted image 20260508192230.png]]](https://i-blog.csdnimg.cn/direct/0c5d170a50814494b8fea3f0208ba077.png)
![![[Pasted image 20260508192242.png]]](https://i-blog.csdnimg.cn/direct/571c5720da7a4daab125662851b98498.png)
![![[Pasted image 20260508192255.png]]](https://i-blog.csdnimg.cn/direct/f36e385c85de468e950bd6f9c414d0f1.png)
合并查询
- union
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行 - 将工资大于2500或职位是MANAGER的人找出来
select ename, sal, job from EMP where sal>2500 union -> select ename, sal, job from EMP where job='MANAGER';--去掉了重复记录
![![[Pasted image 20260508192543.png]]](https://i-blog.csdnimg.cn/direct/005e1720498c4ce6bb10f0f18f37d647.png)
- union all
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。 - 将工资大于25000或职位是MANAGER的人找出来
select ename, sal, job from EMP where sal>2500 union all -> select ename, sal, job from EMP where job='MANAGER';
![![[Pasted image 20260508192618.png]]](https://i-blog.csdnimg.cn/direct/ed6667ec96e04608b9ed757aeb7c1a62.png)
1082



被折叠的 条评论
为什么被折叠?



