赞
踩
什么是子查询??
很简单,其实就是select语句中嵌套select语句,被嵌套的语句称为子查询
子查询都可以出现在哪里呢?
其实子查询在select ,from,where 语句后都可以出现,结构如下:
select
....(select)
from
....(select)
where
....(select)
- mysql> select * from emp;
- +-------+--------+-----------+------+------------+---------+---------+--------+
- | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
- +-------+--------+-----------+------+------------+---------+---------+--------+
- | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
- | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
- | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
- | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
- | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
- | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
- | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
- | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
- | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
- | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
- | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
- | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
- | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
- | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
- +-------+--------+-----------+------+------------+---------+---------+--------+
- 14 rows in set (0.01 sec)
- mysql> select min(sal) from emp;
- +----------+
- | min(sal) |
- +----------+
- | 800.00 |
- +----------+
- 1 row in set (0.00 sec)
- mysql> select ename as '姓名',sal as '工资' from emp where sal >800;
- +--------+---------+
- | 姓名 | 工资 |
- +--------+---------+
- | ALLEN | 1600.00 |
- | WARD | 1250.00 |
- | JONES | 2975.00 |
- | MARTIN | 1250.00 |
- | BLAKE | 2850.00 |
- | CLARK | 2450.00 |
- | SCOTT | 3000.00 |
- | KING | 5000.00 |
- | TURNER | 1500.00 |
- | ADAMS | 1100.00 |
- | JAMES | 950.00 |
- | FORD | 3000.00 |
- | MILLER | 1300.00 |
- +--------+---------+
- 13 rows in set (0.00 sec)
- mysql> select ename as '姓名',sal as '工资' from emp where sal >(select min(sal) from emp );
- +--------+---------+
- | 姓名 | 工资 |
- +--------+---------+
- | ALLEN | 1600.00 |
- | WARD | 1250.00 |
- | JONES | 2975.00 |
- | MARTIN | 1250.00 |
- | BLAKE | 2850.00 |
- | CLARK | 2450.00 |
- | SCOTT | 3000.00 |
- | KING | 5000.00 |
- | TURNER | 1500.00 |
- | ADAMS | 1100.00 |
- | JAMES | 950.00 |
- | FORD | 3000.00 |
- | MILLER | 1300.00 |
- +--------+---------+
- 13 rows in set (0.00 sec)
注意:from 后面的子查询,可以将子查询的查询结果当作一张临时表。(技巧)
- mysql> select * from emp;
- +-------+--------+-----------+------+------------+---------+---------+--------+
- | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
- +-------+--------+-----------+------+------------+---------+---------+--------+
- | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
- | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
- | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
- | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
- | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
- | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
- | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
- | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
- | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
- | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
- | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
- | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
- | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
- | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
- +-------+--------+-----------+------+------------+---------+---------+--------+
- 14 rows in set (0.01 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 job,avg(sal) avgsal from emp group by job;
- +-----------+-------------+
- | job | avgsal |
- +-----------+-------------+
- | CLERK | 1037.500000 |
- | SALESMAN | 1400.000000 |
- | MANAGER | 2758.333333 |
- | ANALYST | 3000.000000 |
- | PRESIDENT | 5000.000000 |
- +-----------+-------------+
- 5 rows in set (0.00 sec)
第二步:克服心理障碍,把以上的查询结果当作一张真实存在的表,并可以命名为 ‘ t ’
t表和salgrade表进行表连接,条件:t 表avg(sal) between losal and hisal
第三步:合并
(注意:若是在 from 后面的子查询中有聚集函数,则必须要给其取别名,不然会报错)
- mysql> select t.*,s.grade
- -> from (select job,avg(sal) avgsal from emp group by job) t
- -> join salgrade s
- -> on t.avgsal between s.losal and s.hisal;
- +-----------+-------------+-------+
- | job | avgsal | grade |
- +-----------+-------------+-------+
- | CLERK | 1037.500000 | 1 |
- | SALESMAN | 1400.000000 | 2 |
- | MANAGER | 2758.333333 | 4 |
- | ANALYST | 3000.000000 | 4 |
- | PRESIDENT | 5000.000000 | 5 |
- +-----------+-------------+-------+
- 5 rows in set (0.00 sec)
案例: 利用找出每个员工的部门名称,要求显示员工名,部门名?
第一步:先查员工的员工名,部门号
- mysql> select e.ename,e.deptno from emp e;
- +--------+--------+
- | ename | deptno |
- +--------+--------+
- | SMITH | 20 |
- | ALLEN | 30 |
- | WARD | 30 |
- | JONES | 20 |
- | MARTIN | 30 |
- | BLAKE | 30 |
- | CLARK | 10 |
- | SCOTT | 20 |
- | KING | 10 |
- | TURNER | 30 |
- | ADAMS | 20 |
- | JAMES | 30 |
- | FORD | 20 |
- | MILLER | 10 |
- +--------+--------+
- 14 rows in set (0.00 sec)
第二步:在查每个员工的部门名称
- mysql> select e.ename,e.deptno,(select d.dname from dept d where e.deptno=d.deptno) as dname from emp e;
- +--------+--------+------------+
- | ename | deptno | dname |
- +--------+--------+------------+
- | SMITH | 20 | RESEARCH |
- | ALLEN | 30 | SALES |
- | WARD | 30 | SALES |
- | JONES | 20 | RESEARCH |
- | MARTIN | 30 | SALES |
- | BLAKE | 30 | SALES |
- | CLARK | 10 | ACCOUNTING |
- | SCOTT | 20 | RESEARCH |
- | KING | 10 | ACCOUNTING |
- | TURNER | 30 | SALES |
- | ADAMS | 20 | RESEARCH |
- | JAMES | 30 | SALES |
- | FORD | 20 | RESEARCH |
- | MILLER | 10 | ACCOUNTING |
- +--------+--------+------------+
- 14 rows in set (0.00 sec)
注意:对于select后面的子查询来说,一次只能返回一条结果,多余一条记录,就报错了!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。