目录
一、基本查询
1、查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J
2、升序降序
2.1按照部门号升序而雇员的工资降序排序
2.2使用年薪进行降序排序
3、子查询
3.1显示工资最高的员工的名字和工作岗位
3.2显示工资高于平均工资的员工信息
4、聚合查询
4.1显示每个部门的平均工资和最高工资(保留两位小数)
4.2显示平均工资低于2000的部门号和它的平均工资(聚合+having)
4.3显示每种岗位的雇员总数,平均工资(聚合+内置函数count)
二、多表查询
1、显示雇员名、雇员工资以及所在部门的名字
2、显示部门号为10的部门名,员工名和工资
3、显示各个员工的姓名,工资,及工资级别
三、自连接
1、显示员工FORD的上级领导的编号和姓名(自查询)
四、子查询
1、在where子句中充当判断条件
1.1单行子查询
1.1.1显示SMITH同一部门的员工
1.2多行子查询
1.2.1in关键字;查询和10号部门的工作岗位相同的雇员的名字,岗位,工资,部门号,但是不包含10自己的
1.2.2查询和10号部门的工作岗位相同的雇员的名字,岗位,工资,部门号,但是不包含10自己的。并且打印出部门的名字(多表查询)
1.2.3all关键字;显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号
1.2.4any关键字;显示工资比部门30的任意员工的工资高的员工的姓名、工资和部门号(包含自己部门的员工)
1.3多列子查询
1.3.1in关键字;查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
2、在from子句中充当笛卡尔积
2.1显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资
2.2显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资、部门地点
2.3查找每个部门工资最高的人的姓名、工资、部门、最高工资
2.4显示每个部门的信息(部门名,编号,地址)和人员数量
五、合并查询
1、union(并集+去重)
1.1找出工资大于2500或职位是MANAGER的人
2、union all(并集+不去重)
2.1将工资大于25000或职位是MANAGER的人找出来
mysql> select* from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.01 sec)
mysql> select ename,job,sal from emp where ename like 'j%' and (sal>500 or job='MANAGER');
+-------+---------+---------+
| ename | job | sal |
+-------+---------+---------+
| JONES | MANAGER | 2975.00 |
| JAMES | CLERK | 950.00 |
+-------+---------+---------+
2 rows in set (0.00 sec)
mysql> select * from emp order by deptno asc,sal desc;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.02 sec)
mysql> select *,sal*12+ifnull(comm,0) 年薪 from emp order by 年薪 desc;
+--------+--------+-----------+------+---------------------+---------+---------+--------+----------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno | 年薪 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+----------+
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 60000.00 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 36000.00 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 36000.00 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 35700.00 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 34200.00 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 29400.00 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 19500.00 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 18000.00 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 16400.00 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 15600.00 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 15500.00 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 13200.00 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 11400.00 |
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 9600.00 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+----------+
mysql> select ename,mgr,sal from emp where sal=(select max(sal) from emp);
+-------+------+---------+
| ename | mgr | sal |
+-------+------+---------+
| KING | NULL | 5000.00 |
+-------+------+---------+
1 row in set (0.01 sec)
where条件筛选中再使用select进行筛选。
mysql> select ename,sal from emp where sal>(select avg(sal) from emp);
+-------+---------+
| ename | sal |
+-------+---------+
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
+-------+---------+
6 rows in set (0.00 sec)
mysql> select deptno,format(avg(sal),2),max(sal) from emp group by deptno;
+--------+--------------------+----------+
| deptno | format(avg(sal),2) | max(sal) |
+--------+--------------------+----------+
| 10 | 2,916.67 | 5000.00 |
| 20 | 2,175.00 | 3000.00 |
| 30 | 1,566.67 | 2850.00 |
+--------+--------------------+----------+
3 rows in set (0.01 sec)
mysql> select deptno,avg(sal) from emp group by deptno having avg(sal)<2000;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)
mysql> select job,avg(sal),count(job) from emp group by job;
+-----------+-------------+------------+
| job | avg(sal) | count(job) |
+-----------+-------------+------------+
| ANALYST | 3000.000000 | 2 |
| CLERK | 1037.500000 | 4 |
| MANAGER | 2758.333333 | 3 |
| PRESIDENT | 5000.000000 | 1 |
| SALESMAN | 1400.000000 | 4 |
+-----------+-------------+------------+
5 rows in set (0.00 sec)
实际开发中往往数据来自不同的表,所以需要多表查询。本节我们用一个简单的公司管理系统,有三张表EMP,DEPT,SALGRADE来演示如何进行多表查询。
mysql> select* from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)
mysql> select* from dept;
+--------+------------+----------+
| deptno | dname | loc |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.00 sec)
emp和dept组合后,emp中的每一行变成了四行,发生了数据的穷举组合。像这种不加过滤条件直接组合的形式称为笛卡尔积。使用多表使用笛卡尔积后,后面必定紧跟一个where子句来确定多张表共同的字段,筛掉多张表中排列组合出来的无效数据,仅留下有效数据供程序员排查。
这样,多表查询问题就转化成了单表查询。
mysql> select* from emp,dept where emp.deptno=dept.deptno;
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | dname | loc |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | SALES | CHICAGO |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | SALES | CHICAGO |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | SALES | CHICAGO |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | SALES | CHICAGO |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | SALES | CHICAGO |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | SALES | CHICAGO |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | RESEARCH | DALLAS |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | ACCOUNTING | NEW YORK |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
14 rows in set (0.00 sec)
mysql> select emp.ename,emp.sal,dept.dname from emp,dept where emp.deptno=dept.deptno;
+--------+---------+------------+
| ename | sal | dname |
+--------+---------+------------+
| SMITH | 800.00 | RESEARCH |
| ALLEN | 1600.00 | SALES |
| WARD | 1250.00 | SALES |
| JONES | 2975.00 | RESEARCH |
| MARTIN | 1250.00 | SALES |
| BLAKE | 2850.00 | SALES |
| CLARK | 2450.00 | ACCOUNTING |
| SCOTT | 3000.00 | RESEARCH |
| KING | 5000.00 | ACCOUNTING |
| TURNER | 1500.00 | SALES |
| ADAMS | 1100.00 | RESEARCH |
| JAMES | 950.00 | SALES |
| FORD | 3000.00 | RESEARCH |
| MILLER | 1300.00 | ACCOUNTING |
+--------+---------+------------+
14 rows in set (0.00 sec)
通过emp.deptno=dept.deptno对两表进行关联。
mysql> select emp.deptno,dept.dname,ename,sal from emp,dept where emp.deptno=dept.deptno and dept.deptno=10;
+--------+------------+--------+---------+
| deptno | dname | ename | sal |
+--------+------------+--------+---------+
| 10 | ACCOUNTING | CLARK | 2450.00 |
| 10 | ACCOUNTING | KING | 5000.00 |
| 10 | ACCOUNTING | MILLER | 1300.00 |
+--------+------------+--------+---------+
3 rows in set (0.00 sec)
--工资等级表
mysql> select* from salgrade;
+-------+-------+-------+
| grade | losal | hisal |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 9999 |
+-------+-------+-------+
5 rows in set (0.00 sec)
mysql> select ename,sal,grade from emp,salgrade where sal>losal and sal
自连接是指在同一张表连接查询。
对同一张表做重命名,在同一张表连接查询:
--可以对同一张表做重命名
mysql> select* from salgrade as t1,salgrade as t2;
+-------+-------+-------+-------+-------+-------+
| grade | losal | hisal | grade | losal | hisal |
+-------+-------+-------+-------+-------+-------+
| 1 | 700 | 1200 | 1 | 700 | 1200 |
| 2 | 1201 | 1400 | 1 | 700 | 1200 |
| 3 | 1401 | 2000 | 1 | 700 | 1200 |
| 4 | 2001 | 3000 | 1 | 700 | 1200 |
| 5 | 3001 | 9999 | 1 | 700 | 1200 |
| 1 | 700 | 1200 | 2 | 1201 | 1400 |
| 2 | 1201 | 1400 | 2 | 1201 | 1400 |
| 3 | 1401 | 2000 | 2 | 1201 | 1400 |
| 4 | 2001 | 3000 | 2 | 1201 | 1400 |
| 5 | 3001 | 9999 | 2 | 1201 | 1400 |
| 1 | 700 | 1200 | 3 | 1401 | 2000 |
| 2 | 1201 | 1400 | 3 | 1401 | 2000 |
| 3 | 1401 | 2000 | 3 | 1401 | 2000 |
| 4 | 2001 | 3000 | 3 | 1401 | 2000 |
| 5 | 3001 | 9999 | 3 | 1401 | 2000 |
| 1 | 700 | 1200 | 4 | 2001 | 3000 |
| 2 | 1201 | 1400 | 4 | 2001 | 3000 |
| 3 | 1401 | 2000 | 4 | 2001 | 3000 |
| 4 | 2001 | 3000 | 4 | 2001 | 3000 |
| 5 | 3001 | 9999 | 4 | 2001 | 3000 |
| 1 | 700 | 1200 | 5 | 3001 | 9999 |
| 2 | 1201 | 1400 | 5 | 3001 | 9999 |
| 3 | 1401 | 2000 | 5 | 3001 | 9999 |
| 4 | 2001 | 3000 | 5 | 3001 | 9999 |
| 5 | 3001 | 9999 | 5 | 3001 | 9999 |
+-------+-------+-------+-------+-------+-------+
25 rows in set (0.00 sec)
mysql> select ename,empno from emp where empno=(select mgr from emp where ename='FORD');
+-------+--------+
| ename | empno |
+-------+--------+
| JONES | 007566 |
+-------+--------+
1 row in set (0.00 sec)
mysql> select t2.ename,t2.empno from emp as t1,emp as t2 where t1.ename='FORD' and t1.mgr=t2.empno;
+-------+--------+
| ename | empno |
+-------+--------+
| JONES | 007566 |
+-------+--------+
1 row in set (0.01 sec)
两种方法都可以。
MySQL中一切皆表,子查询在where子句中使用,充当判断条件,在from子句中出现,充当笛卡尔积。
子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询。
返回一行记录的子查询
mysql> select deptno from emp where ename='SMITH';
+--------+
| deptno |
+--------+
| 20 |
+--------+
1 row in set (0.01 sec)
mysql> select ename,deptno from emp where deptno=20;
+-------+--------+
| ename | deptno |
+-------+--------+
| SMITH | 20 |
| JONES | 20 |
| SCOTT | 20 |
| ADAMS | 20 |
| FORD | 20 |
+-------+--------+
5 rows in set (0.00 sec)
mysql> select ename,deptno from emp where deptno=(select deptno from emp where ename='SMITH');
+-------+--------+
| ename | deptno |
+-------+--------+
| SMITH | 20 |
| JONES | 20 |
| SCOTT | 20 |
| ADAMS | 20 |
| FORD | 20 |
+-------+--------+
5 rows in set (0.00 sec)
实在想不出来怎么写,就把条件进行拆解,再进行重组,你就知道怎么写了。
1、筛选出SMITH的部门;
2、在表中筛选出部门编号为20的员工名和编号;
3、将1和2两句SQL的筛选条件组合。
返回多行记录的子查询
mysql> select distinct job from emp where deptno=10;
+-----------+
| job |
+-----------+
| MANAGER |
| PRESIDENT |
| CLERK |
+-----------+
3 rows in set (0.00 sec)
mysql> select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10) and deptno!=10;
+-------+---------+---------+--------+
| ename | job | sal | deptno |
+-------+---------+---------+--------+
| JONES | MANAGER | 2975.00 | 20 |
| BLAKE | MANAGER | 2850.00 | 30 |
| SMITH | CLERK | 800.00 | 20 |
| ADAMS | CLERK | 1100.00 | 20 |
| JAMES | CLERK | 950.00 | 30 |
+-------+---------+---------+--------+
5 rows in set (0.01 sec)
mysql> select* from (select ename,job,sal,deptno from emp where job in(select distinct job from emp where deptno=10) and deptno!=10) as 新表,dept where dept.deptno=新表.deptno;
+-------+---------+---------+--------+--------+----------+---------+
| ename | job | sal | deptno | deptno | dname | loc |
+-------+---------+---------+--------+--------+----------+---------+
| SMITH | CLERK | 800.00 | 20 | 20 | RESEARCH | DALLAS |
| JONES | MANAGER | 2975.00 | 20 | 20 | RESEARCH | DALLAS |
| ADAMS | CLERK | 1100.00 | 20 | 20 | RESEARCH | DALLAS |
| BLAKE | MANAGER | 2850.00 | 30 | 30 | SALES | CHICAGO |
| JAMES | CLERK | 950.00 | 30 | 30 | SALES | CHICAGO |
+-------+---------+---------+--------+--------+----------+---------+
5 rows in set (0.00 sec)
--筛选出30号部门的所有工资(去重)
mysql> select distinct sal from emp where deptno=30;
+---------+
| sal |
+---------+
| 1600.00 |
| 1250.00 |
| 2850.00 |
| 1500.00 |
| 950.00 |
+---------+
5 rows in set (0.00 sec)
--筛选出大于30号部门所有人的工资的人的信息
mysql> select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);
+-------+---------+--------+
| ename | sal | deptno |
+-------+---------+--------+
| JONES | 2975.00 | 20 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| FORD | 3000.00 | 20 |
+-------+---------+--------+
4 rows in set (0.00 sec)
mysql> select ename,sal,deptno from emp where sal>any(select distinct sal from emp where deptno=30);
+--------+---------+--------+
| ename | sal | deptno |
+--------+---------+--------+
| ALLEN | 1600.00 | 30 |
| WARD | 1250.00 | 30 |
| JONES | 2975.00 | 20 |
| MARTIN | 1250.00 | 30 |
| BLAKE | 2850.00 | 30 |
| CLARK | 2450.00 | 10 |
| SCOTT | 3000.00 | 20 |
| KING | 5000.00 | 10 |
| TURNER | 1500.00 | 30 |
| ADAMS | 1100.00 | 20 |
| FORD | 3000.00 | 20 |
| MILLER | 1300.00 | 10 |
+--------+---------+--------+
12 rows in set (0.00 sec)
单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言
的,而多列子查询则是指查询返回多个列数据的子查询语句。
--筛选出smith的部门和岗位
mysql> select deptno,job from emp where ename='SMITH';
+--------+-------+
| deptno | job |
+--------+-------+
| 20 | CLERK |
+--------+-------+
1 row in set (0.00 sec)
--根据smith筛选出来的部门和岗位,筛选出相同部门及岗位的同事信息
mysql> select ename,deptno,job from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH') and ename!='SMITH';
+-------+--------+-------+
| ename | deptno | job |
+-------+--------+-------+
| ADAMS | 20 | CLERK |
+-------+--------+-------+
1 row in set (0.00 sec)
mysql> select ename,deptno,job from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH') and ename!='SMITH';
注意这句SQL也可以把=改成in
mysql> select ename,deptno,job from emp where (deptno,job)in(select deptno,job from emp where ename='SMITH') and ename!='SMITH';
子查询语句出现在from子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用。
--1、先筛选出每个部门的平均工资
mysql> select deptno,avg(sal) from emp group by deptno;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.00 sec)
--多表查询
mysql> select* from emp,(select deptno,avg(sal) 平均工资 from emp group by deptno) 临时表 where emp.deptno=临时表.deptno;
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+-------------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno | deptno | avg(sal) |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+-------------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 | 20 | 2175.000000 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 | 30 | 1566.666667 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 | 30 | 1566.666667 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 | 20 | 2175.000000 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 | 30 | 1566.666667 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 | 30 | 1566.666667 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 | 10 | 2916.666667 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 | 20 | 2175.000000 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 | 10 | 2916.666667 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 | 30 | 1566.666667 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 | 20 | 2175.000000 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 | 30 | 1566.666667 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 | 20 | 2175.000000 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 | 10 | 2916.666667 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+-------------+
14 rows in set (0.01 sec)
--问题变为单表查询
mysql> select ename,emp.deptno,sal,平均工资 from emp,(select deptno,avg(sal) 平均工资 from emp group by deptno) 临时表 where emp.deptno=临时表.deptno and sal>平均工资;
+-------+--------+---------+--------------+
| ename | deptno | sal | 平均工资 |
+-------+--------+---------+--------------+
| ALLEN | 30 | 1600.00 | 1566.666667 |
| JONES | 20 | 2975.00 | 2175.000000 |
| BLAKE | 30 | 2850.00 | 1566.666667 |
| SCOTT | 20 | 3000.00 | 2175.000000 |
| KING | 10 | 5000.00 | 2916.666667 |
| FORD | 20 | 3000.00 | 2175.000000 |
+-------+--------+---------+--------------+
6 rows in set (0.00 sec)
--找到部门对应的办公地点表dept
mysql> select* from dept;
+--------+------------+----------+
| deptno | dname | loc |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.01 sec)
--将第一问的SQL表和办公地点表dept进行多表查询
mysql> select ename,dept.deptno,sal,平均工资,loc from dept,(select ename,emp.deptno,sal,平均工资 from emp,(select deptno,avg(sal) 平均工资 from emp group by deptno) 临时表 where emp.deptno=临时表
.deptno and sal>平均工资) hignSal where dept.deptno=hignSal.deptno;
+-------+--------+---------+--------------+----------+
| ename | deptno | sal | 平均工资 | loc |
+-------+--------+---------+--------------+----------+
| ALLEN | 30 | 1600.00 | 1566.666667 | CHICAGO |
| JONES | 20 | 2975.00 | 2175.000000 | DALLAS |
| BLAKE | 30 | 2850.00 | 1566.666667 | CHICAGO |
| SCOTT | 20 | 3000.00 | 2175.000000 | DALLAS |
| KING | 10 | 5000.00 | 2916.666667 | NEW YORK |
| FORD | 20 | 3000.00 | 2175.000000 | DALLAS |
+-------+--------+---------+--------------+----------+
6 rows in set (0.00 sec)
--通过group by聚合找出各部门的最高工资
mysql> select deptno,max(sal) from emp group by deptno;
+--------+----------+
| deptno | max(sal) |
+--------+----------+
| 10 | 5000.00 |
| 20 | 3000.00 |
| 30 | 2850.00 |
+--------+----------+
3 rows in set (0.00 sec)
--根据两张表的公共字段deptno,筛选出字段匹配的表
mysql> select ename,sal,emp.deptno,部门最高工资 from emp,(select deptno,max(sal) 部门最高工资 from emp group by deptno) maxSal where maxSal.deptno=emp.deptno;
+--------+---------+--------+--------------------+
| ename | sal | deptno | 部门最高工资 |
+--------+---------+--------+--------------------+
| SMITH | 800.00 | 20 | 3000.00 |
| ALLEN | 1600.00 | 30 | 2850.00 |
| WARD | 1250.00 | 30 | 2850.00 |
| JONES | 2975.00 | 20 | 3000.00 |
| MARTIN | 1250.00 | 30 | 2850.00 |
| BLAKE | 2850.00 | 30 | 2850.00 |
| CLARK | 2450.00 | 10 | 5000.00 |
| SCOTT | 3000.00 | 20 | 3000.00 |
| KING | 5000.00 | 10 | 5000.00 |
| TURNER | 1500.00 | 30 | 2850.00 |
| ADAMS | 1100.00 | 20 | 3000.00 |
| JAMES | 950.00 | 30 | 2850.00 |
| FORD | 3000.00 | 20 | 3000.00 |
| MILLER | 1300.00 | 10 | 5000.00 |
+--------+---------+--------+--------------------+
14 rows in set (0.00 sec)
--将最高工资表和emp表进行多表查询
mysql> select ename,sal,emp.deptno,部门最高工资 from emp,(select deptno,max(sal) 部门最高工资 from emp group by deptno) maxSal where maxSal.deptno=emp.deptno and 部门最高工资=emp.sal;
+-------+---------+--------+--------------------+
| ename | sal | deptno | 部门最高工资 |
+-------+---------+--------+--------------------+
| BLAKE | 2850.00 | 30 | 2850.00 |
| SCOTT | 3000.00 | 20 | 3000.00 |
| KING | 5000.00 | 10 | 5000.00 |
| FORD | 3000.00 | 20 | 3000.00 |
+-------+---------+--------+--------------------+
4 rows in set (0.00 sec)
方案一:使用多表
mysql> select dept.dname, dept.deptno, dept.loc,count(*) '部门人数' from emp,dept where emp.deptno=dept.deptno group by dept.deptno,dept.dname,dept.loc;
+------------+--------+----------+--------------+
| dname | deptno | loc | 部门人数 |
+------------+--------+----------+--------------+
| ACCOUNTING | 10 | NEW YORK | 3 |
| RESEARCH | 20 | DALLAS | 5 |
| SALES | 30 | CHICAGO | 6 |
+------------+--------+----------+--------------+
3 rows in set (0.00 sec)
方案二:子查询:先筛选出临时表,再进行多表查询
mysql> select* from dept;
+--------+------------+----------+
| deptno | dname | loc |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.01 sec)
mysql> select deptno,count(*) from emp group by deptno;
+--------+--------------+
| deptno | count(*) |
+--------+--------------+
| 10 | 3 |
| 20 | 5 |
| 30 | 6 |
+--------+--------------+
3 rows in set (0.00 sec)
mysql> select dept.dname,dept.deptno,dept.loc,人员数量 from dept,(select deptno,count(*) 人员数量 from emp group by deptno) info where dept.deptno=info.deptno;
+------------+--------+----------+--------------+
| dname | deptno | loc | 人员数量 |
+------------+--------+----------+--------------+
| ACCOUNTING | 10 | NEW YORK | 3 |
| RESEARCH | 20 | DALLAS | 5 |
| SALES | 30 | CHICAGO | 6 |
+------------+--------+----------+--------------+
3 rows in set (0.00 sec)
在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all。注意左右两边的select所查询的字段要相同。
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
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 |
+-------+---------+-----------+
6 rows in set (0.00 sec)
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
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 |
+-------+---------+-----------+
8 rows in set (0.01 sec)