• 二十三、MySQL子查询


    1、MySQL子查询

    (1)基础概念:

     (2)分类:

    ·标量子查询

    ·列子查询

    ·行子查询

    ·表子查询

    2、标量子查询

    (1)基础概念:

     (2)实际操作:

    自查询返回的结果为单个值,最简单的形式,这种子查询的称为标量子查询。

    初始表格:

     查询所有年龄比hom大的员工:

    1. # 查询所有年龄比hom大的员工
    2. select * from emp where age>(select age from emp where name='hom');

     3、列子查询

    (1)基础概念:

    (2) 基础表格:

    基础表格emp和course,emp的type字段和course的id字段连接在一起:

     

     (3)实际操作in:

    1. -- (1)in():符合括号中的所有元素
    2. # 查询course表中的技术部和科研部的编号
    3. select id from course where name='技术部' or name='科研部';
    4. # 查询emp表中的type=13的员工
    5. select * from emp where type in(1,3);
    6. # 根据emp和course两个表,查询技术部和科研部的员工
    7. select * from emp where type in(select id from course where name='技术部' or name='科研部');

    (4)实际操作all:

    1. -- (2)all():满足子查询返回的所有结果
    2. # 查询所有销售部的人员年龄
    3. select id from course where name='销售部';
    4. -- 得到销售部的部门ID为4
    5. select age from emp where type=(select id from course where name='销售部');
    6. # 查询比销售部所有人年龄都高的员工信息
    7. select * from emp where age>all(select age from emp where type=(select id from course where name='销售部'));

    (5)实际操作any/some:

    1. -- (3)any()/some():满足子查询返回的任意一个结果
    2. # 查询管理部的员工的部门ID
    3. select id from course where name='管理部';
    4. -- 得到管理部的员工的部门ID=2之后,输出所有管理部员工的年龄
    5. select age from emp where type=(select id from course where name='管理部');
    6. -- 查询比管理部任意一人年龄都高的员工信息
    7. select * from emp where age>any(select age from emp where type=(select id from course where name='管理部'));

    4、 行子查询

    (1)基础概念:

     (2)基础表格:

    (3) 实际操作:

            当用户需要查询与jom的年龄和部门相同的员工时,需要先查询jom的年龄和jom和所属部门的id,再查寻符合age和type的员工。

    1. # 查询jom的年龄age和所属部门type
    2. select age, type from emp where name='jom';
    3. # 得到age=35,以及type=2,再查询与jom的年龄age和所属部门type相同的员工
    4. select * from emp where age=35 and type=2;

     但使用行子查询,是需要使用如下语句即可:

    1. # 行子查询操作:
    2. select * from emp where (age,type)=(select age, type from emp where name='jom');

    5、表子查询

    (1)基础概念: 

            子查询返回的结果是多行多列,也就是一张表,这种子查询被称为表子查询。

    (2)实际操作:

    查询与’jom‘、’kom’的所属部门和年龄相同的员工信息:

    1. -- 1、查询与’jom‘、’kom’的所属部门和年龄相同的员工信息
    2. # 第一步,查询jom和kom的所属部门和年龄
    3. select age,type from emp where name='jom' or name='kom';
    4. # 第二步,查询与jom和kom的所属部门和年龄相同的员工信息
    5. select * from emp where (age,type) in (select age,type from emp where name='jom' or name='kom');

    查询sum在10006之后的员工信息,其部门所属部门信息:

    1. -- 2、查询sum在10006之后的员工信息,其部门所属部门信息
    2. # 第一步,查询sum在10006之后的员工信息
    3. select * from emp where sum>10006;
    4. # 第二步,将第一步子查询的结果(一张表)作为传入参数传入,查询该部分员工对应的部门信息
    5. select e.*,c.* from (select * from emp where sum>10006) as e left join course as c on e.type=c.id;

  • 相关阅读:
    RKMPP库快速上手--(一)RKMPP功能及使用详解
    Java版企业工程项目管理系统源码+java版本+项目模块功能清单+spring cloud +spring boot
    Cadence 16.6 PCB Edito如何将鼠标中键反向拉拽改为正向拖拽
    263.3的幂
    [思维][组合数学]Madoka and The Corruption Scheme Codeforces1717D
    shell编程基础
    Linux-centos
    Go-Excelize API源码阅读(十)—— SetActiveSheet(index int)
    面试高频的CMS回收器
    Hdu 3549 Flow Problem(最大流)
  • 原文地址:https://blog.csdn.net/2301_79149013/article/details/133046769