# select * from EMP where (sal>500 or job='MANAGER') and ename like 'J%';
# select * from EMP order by deptno, sal desc;
# select ename, job from EMP where sal = (select max(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 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;
//使用的子查询:
# 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';
单行子查询:返回一行记录的子查询
# 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 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子句中使用子查询;MySQL中一切皆表格。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.deptno, dname, mycnt, loc from DEPT,
(select count(*) mycnt, deptno from EMP group by deptno) tmp
where DEPT.deptno=tmp.deptno;
//2:
# select DEPT.dname, DEPT.deptno, DEPT.loc,count(*) '部门人数' from EMP, DEPT
where EMP.deptno=DEPT.deptno
group by DEPT.deptno,DEPT.dname,DEPT.loc;//前面select出现的字段名,在group by后面也要出现。
union
# mysql> select ename, sal, job from EMP where sal>2500 union select ename, sal, job
from EMP where job='MANAGER';
//去掉了重复记录
union all
# mysql> select ename, sal, job from EMP where sal>2500 union all
select ename, sal, job from EMP where job='MANAGER';
# select 字段 from 表1 inner join 表2 on 连接条件 and 其他条件;
//用标准的内连接写法
# select ename, dname from EMP, DEPT where EMP.deptno=DEPT.deptno and ename='SMITH';
//用标准的内连接写法
# select ename, dname from EMP inner join DEPT on EMP.deptno=DEPT.deptno and ename='SMITH';
左外连接
select 字段名 from 表名1 left join 表名2 on 连接条件
# create table stu (id int, name varchar(30)); //学生表
# insert into stu values(1,'jack'),(2,'tom'),(3,'kity'),(4,'nono');
# create table exam (id int, grade int); // 成绩表
# insert into exam values(1, 56),(2,76),(11, 8);
// 当左边表和右边表没有匹配时,也会显示左边表的数据
# select * from stu left join exam on stu.id=exam.id;
右外连接
select 字段 from 表名1 right join 表名2 on 连接条件;
# select * from stu right join exam on stu.id=exam.id;
//一:
# select d.dname, e.* from dept d left join emp e on d.deptno=e.deptno;
//二:
# select d.dname, e.* from emp e right join dept d on d.deptno=e.deptno;