当前位置:   article > 正文

你想知道的 MySQL---三种子查询 都在这里!_mysql from子查询

mysql from子查询

一、知识点介绍

   什么是子查询??

很简单,其实就是select语句中嵌套select语句,被嵌套的语句称为子查询

二、分类

子查询都可以出现在哪里呢?

其实子查询在select ,from,where 语句后都可以出现,结构如下:

select 

     ....(select)

from 

     ....(select)

where

     ....(select)

三、实例展示

1.where子句中的子查询

案例:利用下表找出比最低工资高的员工姓名和工资?
 
  1. mysql> select * from emp;
  2. +-------+--------+-----------+------+------------+---------+---------+--------+
  3. | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
  4. +-------+--------+-----------+------+------------+---------+---------+--------+
  5. | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
  6. | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
  7. | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
  8. | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
  9. | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
  10. | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
  11. | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
  12. | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
  13. | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
  14. | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
  15. | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
  16. | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
  17. | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
  18. | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
  19. +-------+--------+-----------+------+------------+---------+---------+--------+
  20. 14 rows in set (0.01 sec)

实现思路:

  第一步:查询最低工资是多少
  1. mysql> select min(sal) from emp;
  2. +----------+
  3. | min(sal) |
  4. +----------+
  5. | 800.00 |
  6. +----------+
  7. 1 row in set (0.00 sec)
  第二步:找出工资>800的
  1. mysql> select ename as '姓名',sal as '工资' from emp where sal >800;
  2. +--------+---------+
  3. | 姓名 | 工资 |
  4. +--------+---------+
  5. | ALLEN | 1600.00 |
  6. | WARD | 1250.00 |
  7. | JONES | 2975.00 |
  8. | MARTIN | 1250.00 |
  9. | BLAKE | 2850.00 |
  10. | CLARK | 2450.00 |
  11. | SCOTT | 3000.00 |
  12. | KING | 5000.00 |
  13. | TURNER | 1500.00 |
  14. | ADAMS | 1100.00 |
  15. | JAMES | 950.00 |
  16. | FORD | 3000.00 |
  17. | MILLER | 1300.00 |
  18. +--------+---------+
  19. 13 rows in set (0.00 sec)
  第三步:合并

  1. mysql> select ename as '姓名',sal as '工资' from emp where sal >(select min(sal) from emp );
  2. +--------+---------+
  3. | 姓名 | 工资 |
  4. +--------+---------+
  5. | ALLEN | 1600.00 |
  6. | WARD | 1250.00 |
  7. | JONES | 2975.00 |
  8. | MARTIN | 1250.00 |
  9. | BLAKE | 2850.00 |
  10. | CLARK | 2450.00 |
  11. | SCOTT | 3000.00 |
  12. | KING | 5000.00 |
  13. | TURNER | 1500.00 |
  14. | ADAMS | 1100.00 |
  15. | JAMES | 950.00 |
  16. | FORD | 3000.00 |
  17. | MILLER | 1300.00 |
  18. +--------+---------+
  19. 13 rows in set (0.00 sec)

2、from 子句中的子查询

 注意:from 后面的子查询,可以将子查询的查询结果当作一张临时表。(技巧

案例: 利用下表找出每个岗位的平均工资的薪资等级。(分别为职工表、薪资登记表)
  1. mysql> select * from emp;
  2. +-------+--------+-----------+------+------------+---------+---------+--------+
  3. | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
  4. +-------+--------+-----------+------+------------+---------+---------+--------+
  5. | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
  6. | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
  7. | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
  8. | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
  9. | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
  10. | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
  11. | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
  12. | 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
  13. | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
  14. | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
  15. | 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
  16. | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
  17. | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
  18. | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
  19. +-------+--------+-----------+------+------------+---------+---------+--------+
  20. 14 rows in set (0.01 sec)
  21. mysql> select * from salgrade;
  22. +-------+-------+-------+
  23. | GRADE | LOSAL | HISAL |
  24. +-------+-------+-------+
  25. | 1 | 700 | 1200 |
  26. | 2 | 1201 | 1400 |
  27. | 3 | 1401 | 2000 |
  28. | 4 | 2001 | 3000 |
  29. | 5 | 3001 | 9999 |
  30. +-------+-------+-------+
  31. 5 rows in set (0.00 sec)

实现思路:

  第一步:找出每个岗位的平均工资(按照岗位分组求平均值)

  1. mysql> select job,avg(sal) avgsal from emp group by job;
  2. +-----------+-------------+
  3. | job | avgsal |
  4. +-----------+-------------+
  5. | CLERK | 1037.500000 |
  6. | SALESMAN | 1400.000000 |
  7. | MANAGER | 2758.333333 |
  8. | ANALYST | 3000.000000 |
  9. | PRESIDENT | 5000.000000 |
  10. +-----------+-------------+
  11. 5 rows in set (0.00 sec)

 第二步:克服心理障碍,把以上的查询结果当作一张真实存在的表,并可以命名为  ‘  t  ’

t表和salgrade表进行表连接,条件:t 表avg(sal) between losal and hisal

 第三步:合并

(注意:若是在 from 后面的子查询中有聚集函数,则必须要给其取别名,不然会报错)

  1. mysql> select t.*,s.grade
  2. -> from (select job,avg(sal) avgsal from emp group by job) t
  3. -> join salgrade s
  4. -> on t.avgsal between s.losal and s.hisal;
  5. +-----------+-------------+-------+
  6. | job | avgsal | grade |
  7. +-----------+-------------+-------+
  8. | CLERK | 1037.500000 | 1 |
  9. | SALESMAN | 1400.000000 | 2 |
  10. | MANAGER | 2758.333333 | 4 |
  11. | ANALYST | 3000.000000 | 4 |
  12. | PRESIDENT | 5000.000000 | 5 |
  13. +-----------+-------------+-------+
  14. 5 rows in set (0.00 sec)

3、select 后面出现的子查询

案例: 利用找出每个员工的部门名称,要求显示员工名,部门名?

 第一步:先查员工的员工名,部门号

  1. mysql> select e.ename,e.deptno from emp e;
  2. +--------+--------+
  3. | ename | deptno |
  4. +--------+--------+
  5. | SMITH | 20 |
  6. | ALLEN | 30 |
  7. | WARD | 30 |
  8. | JONES | 20 |
  9. | MARTIN | 30 |
  10. | BLAKE | 30 |
  11. | CLARK | 10 |
  12. | SCOTT | 20 |
  13. | KING | 10 |
  14. | TURNER | 30 |
  15. | ADAMS | 20 |
  16. | JAMES | 30 |
  17. | FORD | 20 |
  18. | MILLER | 10 |
  19. +--------+--------+
  20. 14 rows in set (0.00 sec)

第二步:在查每个员工的部门名称

  1. mysql> select e.ename,e.deptno,(select d.dname from dept d where e.deptno=d.deptno) as dname from emp e;
  2. +--------+--------+------------+
  3. | ename | deptno | dname |
  4. +--------+--------+------------+
  5. | SMITH | 20 | RESEARCH |
  6. | ALLEN | 30 | SALES |
  7. | WARD | 30 | SALES |
  8. | JONES | 20 | RESEARCH |
  9. | MARTIN | 30 | SALES |
  10. | BLAKE | 30 | SALES |
  11. | CLARK | 10 | ACCOUNTING |
  12. | SCOTT | 20 | RESEARCH |
  13. | KING | 10 | ACCOUNTING |
  14. | TURNER | 30 | SALES |
  15. | ADAMS | 20 | RESEARCH |
  16. | JAMES | 30 | SALES |
  17. | FORD | 20 | RESEARCH |
  18. | MILLER | 10 | ACCOUNTING |
  19. +--------+--------+------------+
  20. 14 rows in set (0.00 sec)

注意:对于select后面的子查询来说,一次只能返回一条结果,多余一条记录,就报错了!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/536220
推荐阅读
相关标签
  

闽ICP备14008679号