select * from EMP where (sal>500 or job='MANAGER') and ename like 'J%';
select * from EMP order by deptno, sal desc;
select ename, sal*12+ifnull(comm,0) as '年薪' from EMP order by 年薪 desc;
select ename, job from EMP where sal = (select max(sal) from EMP);
select ename, sal 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,SALGRADE来演示如何进行多表查询。
案例:
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)
select empno,ename from emp where emp.empno=(select mgr from emp where ename='FORD');
-- 使用到表的别名
select leader.empno,leader.ename from emp leader, emp worker where leader.empno = worker.mgr and worker.ename='FORD';
子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询
返回一行记录的子查询
select * from EMP WHERE deptno = (select deptno from EMP where ename='smith');
返回多行记录的子查询
select ename,job,sal,empno 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 sal from EMP where
deptno=30);
select ename, sal, deptno from EMP where sal > any(select sal from EMP where
deptno=30);
单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言的,而多列子查询则是指查询返回多个列数据的子查询语句
案例:查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
mysql> select ename from EMP where (deptno, job)=(select deptno, job from EMP where ename='SMITH') and ename <> 'SMITH';
+-------+
| ename |
+-------+
| ADAMS |
+-------+
子查询语句出现在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;
使用多表
select DEPT.dname, DEPT.deptno, DEPT.loc,count(*) '部门人数' from EMP, DEPT
where EMP.deptno=DEPT.deptno
group by DEPT.deptno,DEPT.dname,DEPT.loc;
使用子查询
-- 1. 对EMP表进行人员统计
select count(*), deptno from EMP group by deptno;
-- 2. 将上面的表看作临时表
select DEPT.deptno, dname, loc, mycnt from DEPT,
(select count(*) mycnt, deptno from EMP group by deptno) tmp
where DEPT.deptno=tmp.deptno;
在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
案例:将工资大于2500或职位是MANAGER的人找出来
mysql> select ename, sal, job from EMP where sal>2500
-> union
-> select ename, sal, job from EMP where job='MANAGER';--去掉了重复记录
+-------+---------+-----------+
| ename | sal | job |
+-------+---------+-----------+
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| SCOTT | 3000.00 | ANALYST |
| KING | 5000.00 | PRESIDENT |
| FORD | 3000.00 | ANALYST |
| CLARK | 2450.00 | MANAGER |
+-------+---------+-----------+
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
案例:将工资大于25000或职位是MANAGER的人找出来
mysql> select ename, sal, job from EMP where sal>2500
-> union all
-> select ename, sal, job from EMP where job='MANAGER';
+-------+---------+-----------+
| ename | sal | job |
+-------+---------+-----------+
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| SCOTT | 3000.00 | ANALYST |
| KING | 5000.00 | PRESIDENT |
| FORD | 3000.00 | ANALYST |
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| CLARK | 2450.00 | MANAGER |
+-------+---------+-----------+
select emp_no,salary from salaries
group by emp_no having min(from_date)
order by emp_no desc;
select 'select count(*) from'|| name ||';'
from sqlite_master where type='table'
-- 在 SQLite系统表 sqlite_master 中可以获得所有表的索引,其中字段 name 是所有表的名字,
-- 而且对于自己创建的表而言,字段 type 永远是 'table'
select emp_no from employees
where emp_no not in (select emp_no from dept_manager)
select e.emp_no, m.emp_no manager_no from dept_emp e, dept_manager m
where e.dept_no=m.dept_no
and e.to_date='9999-01-01'
and e.to_date=m.to_date
and e.emp_no!=m.emp_no;
如果本篇博文有帮助到您,欢迎点赞激励博主呐~~