前面我们讲解的mysql表的查询都是对一张表进行查询,在实际开发中这远远不够。
回顾基本查询
回顾一下前面所学的基本查询,通过一些案例来练习回顾:
- 查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J
select * from emp where (sal>500 or job='MANAGER') and ename like 'J%';
select * from emp where (sal>500 or job='MANAGER') and substring(ename,1,1)='J';
![image-20230616153215191](https://img-blog.csdnimg.cn/img_convert/06b9ba019c41d2cd5cd9f766bb02370a.png)
select * from emp order by deptno asc,sal desc;
select ename,sal,comm,sal*12+ifnull(comm,0) 年薪 from emp order by 年薪 desc;
![image-20230616154835895](https://img-blog.csdnimg.cn/img_convert/b6ad3b311029be4884848e8a8e420cf4.png)
select * from emp where sal=(select max(sal) from emp);
![image-20230616155417739](https://img-blog.csdnimg.cn/img_convert/b0f372b7ebf7eb1290dd0077e5593cf1.png)
select * from emp where sal > (select avg(sal) from emp);
![image-20230616155553578](https://img-blog.csdnimg.cn/img_convert/11ff17b3c763c5b57b5160dcd4817819.png)
select deptno, max(sal)最高,format(avg(sal),2) 平均 from emp group by deptno;
![image-20230616155911748](https://img-blog.csdnimg.cn/img_convert/1b503444f6c284c1f1e1d1268425a5f2.png)
select deptno,avg(sal) 平均工资 from emp group by deptno having avg(sal)<2000;
-- select deptno,avg(sal) 平均工资 from emp group by deptno having 平均工资<2000;
![image-20230616160210394](https://img-blog.csdnimg.cn/img_convert/f35cc1745d63bcd39b06ddbb44ec482f.png)
select job,count(*) 人数,format(avg(sal),2) 平均工资 from emp group by job;
![image-20230616160906439](https://img-blog.csdnimg.cn/img_convert/9ed301ae2fc86ab717750dc081f99029.png)
多表查询
实际开发中往往数据来自不同的表,所以需要多表查询。现在我们用三张表EMP,DEPT,SALGRADE来演示多表查询。
因为上面的数据来自EMP和DEPT表,因此要联合查询
将数据进行穷举组合——就是笛卡尔积,此时可以理解为把两张表变成了一张表
![image-20230616161929102](https://img-blog.csdnimg.cn/img_convert/0f439ea925cac626abde0ae53b226af0.png)
select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
![image-20230616162255418](https://img-blog.csdnimg.cn/img_convert/10744472b4794435f09efc473c247eac.png)
select ename,sal,dname from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;
![image-20230616162556826](https://img-blog.csdnimg.cn/img_convert/f6906d925d8d37e9886fd31e57ecb3f3.png)
select ename,sal,grade,losal,hisal from emp,salgrade where sal between losal and hisal;
![](https://img-blog.csdnimg.cn/img_convert/7745d69dc2f03d4c8b3a443ae34e8b9f.png)
自连接
自连接是指在同一张表连接查询
举个例子:
- 显示员工FORD的上级领导的编号和姓名(mgr是员工领导的编号–empno)
想找FORD的领导的编号,通过EMP表;根据领导的编号,找领导信息,也是通过EMP表:
使用的子查询
select ename,empno from emp where empno=(select mgr from emp where ename='FORD');
![image-20230616165055200](https://img-blog.csdnimg.cn/img_convert/42e1ca905d269c197da397a4f8f5960e.png)
使用多表查询(自查询):
select e2.ename,e2.empno from emp e1,emp e2 where e1.ename='FORD' and e1.mgr=e2.empno;
![image-20230616165818251](https://img-blog.csdnimg.cn/img_convert/0ee51f106f868b7750e0d0dbf6780ca5.png)
子查询
子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询
单行子查询
返回一行记录的子查询
select * from emp where deptno=(select deptno from emp where ename='SMITH');
![image-20230616232858679](https://img-blog.csdnimg.cn/img_convert/696c5167a84163765d5292cd771c2bef.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;
![image-20230616233529472](https://img-blog.csdnimg.cn/img_convert/261447fb555c95c0588528d93814db0a.png)
如果此时还想知道对应的员工属于哪个部门的名字呢?
select ename,job,sal,dname from (select ename,job,sal,deptno from emp where job in (select distinct jobb from emp where deptno=10) and deptno <> 10) as tmp,dept where dept.deptno=tmp.deptno;
![image-20230616235051124](https://img-blog.csdnimg.cn/img_convert/9f02e1c9950043c306711ab34cfa5a24.png)
- all关键字;显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号
第一种做法:
select * from emp where sal > (select max(sal) from emp where deptno=30);
![image-20230616235453947](https://img-blog.csdnimg.cn/img_convert/8f64dee1aea701136c2bf8995d6f4468.png)
第二种做法:
select * from emp where sal>all(select distinct sal from emp where deptno=30);
![image-20230616235652712](https://img-blog.csdnimg.cn/img_convert/cb172fedd693f43b56250257ba56f06e.png)
- any关键字;显示工资比部门30的任意员工的工资高的员工的姓名、工资和部门号(包含自己部门的员工)
select * from emp where sal > any(select distinct sal from emp where deptno=30);
![image-20230617000822493](https://img-blog.csdnimg.cn/img_convert/513445c02e2d2ce4c7c5d20d9e3bbaf6.png)
多列子查询
单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言的,而多列子查询则是指查询返回多个列数据的子查询语句
- 查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
select * from emp where (deptno,job) = (select deptno,job from emp where ename ='SMITH') and ename <>'SMITH';
-- select * from emp where (deptno,job) in (select deptno,job from emp where ename ='SMITH') and ename <> 'SMITH';
![image-20230617001335480](https://img-blog.csdnimg.cn/img_convert/a7402a21faca6e113e421af1a845f452.png)
我们前面说了,任何时刻,查询出来的临时结构,本质在逻辑上也是表结构!
我们上面的子查询全部都是在where子句中,充当判断条件。
下面,我们来看一看在from子句中使用子查询。
在from子句中使用子查询
子查询语句出现在from子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用。
- 显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资
select * from emp,(select deptno,avg(sal) myavg from emp group by deptno) tmp where emp.deptno=tmp.deptno and emp.sal> tmp.myavg;
![image-20230617213732453](https://img-blog.csdnimg.cn/img_convert/81835624b9b1db79104c9f89c7bfffc1.png)
- 查找每个部门工资最高的人的姓名、工资、部门、最高工资
select ename,sal,t1.deptno,mymax from emp t1,(select deptno,max(sal) mymax from emp group by deptno) t2 where t1.deptno = t2.deptno
and t1.sal=t2.mymax;
![image-20230617214724863](https://img-blog.csdnimg.cn/img_convert/864e5ad56bcdbf80479125378d8bb5a3.png)
- 显示每个部门的信息(部门名,编号,地址)和人员数量
select t1.dname,t1.loc,t2.dept_num,t1.deptno from dept t1,(select deptno, count(*) dept_num from emp group by deptno) t2 where t1.deptno=t2.dtno=t2.deptno;
![image-20230617220021225](https://img-blog.csdnimg.cn/img_convert/aaa2fb5a8898daa81ade8c8ef360e5f5.png)
合并查询
在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all
union
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行
- 将工资大于2500或职位是MANAGER的人找出来
select * from emp where sal > 2500 union select * from emp where job='MANAGER';
![image-20230617224752342](https://img-blog.csdnimg.cn/img_convert/50d0cf0dbf168a5f40878930398aac68.png)
union all
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
select * from emp where sal > 2500 union all select * from emp where job='MANAGER';
![image-20230617224808089](https://img-blog.csdnimg.cn/img_convert/e57df341e9938cd2fff1e793cfe23193.png)
OJ题目
查找所有员工入职时候的薪水情况
描述
查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序(请注意,一个员工可能有多次涨薪的情况)
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
输入描述:
无
![image-20230617225145771](https://img-blog.csdnimg.cn/img_convert/2f982e26d89a74a3abe0bd4a506e1d9f.png)
select s.emp_no,s.salary from salaries s,employees e
where e.emp_no=s.emp_no and e.hire_date=s.from_date
order by e.emp_no desc;