当前位置:   article > 正文

MySQL-多表查询-子查询(标量、列)_mysql 查询子表 数量

mysql 查询子表 数量

子查询

  • 概述

    • 介绍:SQL语句中嵌套select语句,称为嵌套查询,又称子查询
    • 形式:select * from t1 where column1 = (select column1 from t2.....)
    • 子查询外部语句就可以insert/update/delete/select的任何一个,最常见的是select
  • 分类

    • 标量子查询
      • 子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式
        •  
      • 常用符号:=、<>、>、>=、<、<=
      • 具体演示代码如下
          1. -- todo 子查询
          2. -- 标量子查询
          3. -- A.查询“教研部”的所有员工信息
          4. -- a.查询 教研部 的部门id
          5. select tb_dept.id
          6. from tb_dept
          7. where name = '教研部';
          8. -- b.再查询该部门id下的员工信息
          9. select *
          10. from tb_emp
          11. where dept_id = 2;
          12. -- 最终语句如下
          13. select *
          14. from tb_emp
          15. where dept_id = (select id from tb_dept where name = '教研部');
          16. -- select id from tb_dept where name = '教研部' 该语句属于子查询
          17. -- B.查询在“东方白”入职后的员工信息
          18. -- a.查询’东方白‘的入职时间
          19. select entrydate
          20. from tb_emp
          21. where name = '方东白';
          22. -- b.查询在方东白入职之后的员工信息
          23. select *
          24. from tb_emp
          25. where entrydate > '2012-11-01';
          26. -- 最终语句如下
          27. select *
          28. from tb_emp
          29. where entrydate > (select entrydate
          30. from tb_emp
          31. where name = '方东白')
          32. -- 子查询的语句
          33. # select entrydate
          34. # from tb_emp
          35. # where name = '方东白'
    • 列子查询
      • 子查询返回的结果为一列(可以是多行)、

        •  

      • 常用符号:in、not in等
      • 具体演示代码如下
          1. -- todo 列子查询
          2. -- A.查询’教研部‘和’咨询部‘员工的所有信息
          3. -- a.获取两部门的部门id
          4. select id
          5. from tb_dept
          6. where name = '教研部'
          7. or name = '咨询部';
          8. -- b.根据部门id,查询该部门下的员工id
          9. select *
          10. from tb_emp
          11. where dept_id in (2, 3);
          12. -- 合并后的查询语句
          13. select *
          14. from tb_emp
          15. where dept_id in (select id
          16. from tb_dept
          17. where name = '教研部'
          18. or name = '咨询部');
    • 行子查询
      • 子查询返回的结果为一行(可以是多列)

        • 具体演示代码如下

            1. -- todo 行查询
            2. -- A.查询与‘韦一笑’的入职日期及职位都相同的员工信息
            3. -- a.查询‘韦一笑’ 的入职日期 及 职位
            4. select entrydate, job
            5. from tb_emp
            6. where name = '韦一笑';
            7. -- b.查询与‘韦一笑’的入职日期及职位相同的员工信息
            8. select *
            9. from tb_emp
            10. where entrydate = '2007-01-01'
            11. and job = 2;
            12. select *
            13. from tb_emp
            14. where (entrydate, job) = ('2007-01-01', 2);
            15. -- 合并查询语句
            16. select *
            17. from tb_emp
            18. where (entrydate, job) = (select entrydate, job
            19. from tb_emp
            20. where name = '韦一笑');
    • 表子查询
      • 子查询返回的结果为多行多列,常作为临时表

        • 常用的操作符:in

          •  具体操作代码

            1. -- todo 表子查询
            2. -- A.查询入职日期是’2006-01-01‘之后的员工信息,及部门名称
            3. -- a.查询入职日期为’2006-01-01‘之后的员工信息
            4. select *
            5. from tb_emp
            6. where entrydate > '2006-01-01';
            7. -- b.查询这部分员工信息及其部门名称
            8. select *
            9. from (select *
            10. from tb_emp
            11. where entrydate > '2006-01-01') e,
            12. tb_dept d
            13. where e.dept_id = d.id;
            14. # select *
            15. # from tb_emp
            16. # where entrydate > '2006-01-01' 为子查询

             

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

闽ICP备14008679号