genshin-wish-export 原神祈愿记录导出完整指南:一键把抽卡历史变成 Excel
2026/10/29 10:35:03
select * from EMP where (sal>500 or job='MANAGER') and ename like 'J%';select * from EMP order by deptno, sal desc;select ename,sal, comm, sal*12+ifnull(comm,0) as '年薪' from EMP order by 年薪 desc;select ename, job from EMP where sal = (select max(sal) from EMP);select * from EMP where sal>(select avg(sal) from EMP);select deptno, format(avg(sal), 2) , max(sal) from EMP group by deptno;select deptno, avg(sal) as avg_sal from EMP group by deptno having avg_sal<=2000;select job,count(*) 人数, format(avg(sal),2) 平均工资 from EMP group by job;实际开发中往往数据来自不同的表,所以需要多表查询。
注意:因为上面的数据分别来自EMP和DEPT两张不同得表,因此要联合查询
select EMP.ename, EMP.sal, DEPT.dname from EMP, DEPT where EMP.deptno = DEPT.deptno;select ename, sal,dname from EMP, DEPT where EMP.deptno=DEPT.deptno and DEPT.deptno = 10;select ename, sal, grade from EMP, SALGRADE where EMP.sal between losal and hisal;自连接是指在同一张表连接查询
案例:显示员工FORD的上级领导的编号和姓名(mgr是员工领导的编号--empno)
方法一:使用子查询:
第一步:先找ford领导得编号
第二步:在通过领导编号找到领导第三步:合并语句
第三步:合并语句
方法二:使用自连接查询
注意:from emp leader, emp worker,给自己的表起别名,因为要先做笛卡尔积,所以别名可以先识别
select leader.empno,leader.ename from emp leader, emp worker where leader.empno = worker.mgr and worker.ename='FORD';返回一行记录的子查询
案例:显示SMITH同一部门的员工
select * from EMP WHERE deptno = (select deptno from EMP where ename='smith');返回多行记录的子查询
select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10) and deptno<>10;select ename, sal, deptno from EMP where sal > all(select distinct sal from EMP where deptno=30);select ename, sal, deptno from EMP where sal > any(select sal from EMP where deptno=30);单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言 的,而多列子查询则是指查询返回多个列数据的子查询语句。
案例:查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
子查询语句出现在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;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;方法1:使用多表
select DEPT.dname, DEPT.deptno, DEPT.loc,count(*) '部门人数' from EMP, DEPT where EMP.deptno=DEPT.deptno group by DEPT.deptno,DEPT.dname,DEPT.loc;方法2:使用子查询
#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;在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
案例:将工资大于2500或职位是MANAGER的人找出来
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
案例:将工资大于25000或职位是MANAGER的人找出来