MySQL-multi-table query-subquery (scalar, column)

subquery

  • overview

    • Introduction: Nested select statements in SQL statements are called nested queries, also known as subqueries
    • 形式:select * from t1 where column1 = (select column1 from t2.....)
    • The subquery external statement can be any one of insert/update/delete/select, the most common is select
  • Classification

    • scalar subqueries

      • The result returned by a subquery is a single value (number, string, date, etc.), in its simplest form
        •  
      • Common symbols: =, <>, >, >=, <, <=
      • The specific demo code is as follows
        • -- todo 子查询
          -- 标量子查询
          -- A.查询“教研部”的所有员工信息
          -- a.查询 教研部 的部门id
          select tb_dept.id
          from tb_dept
          where name = '教研部';
          -- b.再查询该部门id下的员工信息
          select *
          from tb_emp
          where dept_id = 2;
          -- 最终语句如下
          select *
          from tb_emp
          where dept_id = (select id from tb_dept where name = '教研部');
          -- select id from tb_dept where name = '教研部'   该语句属于子查询
          
          -- B.查询在“东方白”入职后的员工信息
          -- a.查询’东方白‘的入职时间
          select entrydate
          from tb_emp
          where name = '方东白';
          -- b.查询在方东白入职之后的员工信息
          select *
          from tb_emp
          where entrydate > '2012-11-01';
          -- 最终语句如下
          select *
          from tb_emp
          where entrydate > (select entrydate
                             from tb_emp
                             where name = '方东白')
          -- 子查询的语句
          # select entrydate
          # from tb_emp
          # where name = '方东白'
    • column query

      • The result returned by the subquery is a column (can be multiple rows),

        •  

      • Commonly used symbols: in, not in, etc.
      • The specific demo code is as follows
        • -- todo 列子查询
          -- A.查询’教研部‘和’咨询部‘员工的所有信息
          -- a.获取两部门的部门id
          select id
          from tb_dept
          where name = '教研部'
             or name = '咨询部';
          -- b.根据部门id,查询该部门下的员工id
          select *
          from tb_emp
          where dept_id in (2, 3);
          -- 合并后的查询语句
          select *
          from tb_emp
          where dept_id in (select id
                            from tb_dept
                            where name = '教研部'
                               or name = '咨询部');
    • row subquery

      • The result returned by the subquery is one row (can be multiple columns)

        • The specific demo code is as follows

          • -- todo 行查询
            -- A.查询与‘韦一笑’的入职日期及职位都相同的员工信息
            -- a.查询‘韦一笑’ 的入职日期 及 职位
            select entrydate, job
            from tb_emp
            where name = '韦一笑';
            -- b.查询与‘韦一笑’的入职日期及职位相同的员工信息
            select *
            from tb_emp
            where entrydate = '2007-01-01'
              and job = 2;
            
            select *
            from tb_emp
            where (entrydate, job) = ('2007-01-01', 2);
            
            -- 合并查询语句
            select *
            from tb_emp
            where (entrydate, job) = (select entrydate, job
                                      from tb_emp
                                      where name = '韦一笑');
    • table subquery

      • The result returned by the subquery is multi-row and multi-column, often used as a temporary table

        • Commonly used operators: in

          •  Specific operation code

          • -- todo 表子查询
            -- A.查询入职日期是’2006-01-01‘之后的员工信息,及部门名称
            -- a.查询入职日期为’2006-01-01‘之后的员工信息
            select *
            from tb_emp
            where entrydate > '2006-01-01';
            -- b.查询这部分员工信息及其部门名称
            select *
            from (select *
                  from tb_emp
                  where entrydate > '2006-01-01') e,
                 tb_dept d
            where e.dept_id = d.id;
            # select *
            # from tb_emp
            # where entrydate > '2006-01-01' 为子查询

             

Guess you like

Origin blog.csdn.net/weixin_64939936/article/details/131859596