MySql数据库查询(DQL)语言—子查询

2024-05-26 21:18

本文主要是介绍MySql数据库查询(DQL)语言—子查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

什么是子查询

出现在其他语句中的select语句,称为子查询或内查询,外部的查询语句,称为主查询或外查询。

子查询分类

一、按子查询出现的位置分类

  1. select后面:仅支持标量子查询。
  2. from后面:支持表子查询。
  3. where或having后面:支持标量子查询、列子查询、行子查询。
  4. exists(相关子查询)后面:支持表子查询。

二、按结果集的行列数不同分类

  1. 标量子查询(结果集只有一行一列)
  2. 行子查询(结果集只有一行多列)
  3. 列子查询(结果集只有一列多行)
  4. 表子查询(结果集一般为多行多列 )

子查询在where或having后面使用

一、在where或having后面使用特点

  1. 支持标量子查询、列子查询、行子查询

  2. 特点:
    1.子查询放在小括号内;
    2.子查询一般放在条件的右侧;
    3.标量子查询一般搭配着单行操作符使用>,< ,>= ,<=,=,<>;
    4.列子查询一般搭配着多行操作符使用,in,any/some,all;
    5.子查询的执行优先于主查询,因为主查询查询条件是子查询的结果集。
    二、在where后面使用标量子查询(单行单列)

  3. 谁的工资比 Abel高?
    分析:1.首先查询Abel的工资;2.查询员工的信息,满足salary大于1的结果。
    select * from employees where salary > (select salary from employees where last_name = "Abel");

  4. 使用多个子查询
    描述:返回job_id与员工号为141的员工相同,salary比143号员工多的员工姓名,job_id和工资。
    分析:
    第一步:查询job_id与141号员工相同的job_id;
    select job_id from employees where employee_id = 141; // 返回单行单列结果集
    第二步:查询143号员工的salary;
    select salary from employees where employee_id = 143;
    第三步:查询员工的姓名,job_id和工资,要求job_id=第一步结果,salary>第二步结果集;
    select last_name, job_id, salary from employees where job_id = (select job_id from employees where employee_id = 141) and salary > (select salary from employees where employee_id = 143);

  5. 结合分组函数
    描述:返回公司工资最少的员工的last_name, job_id和salary。
    分析:
    第一步:查询公司最低工资;
    select min(salary) from employees;
    第二步:查询员工salary = 第一步的信息;
    select last_name, job_id,salary from employees where salary = (select min(salary) from employees);

二、在having后面使用标量子查询(单行单列)

  1. 查询最低工资大于50号部门最低工资的部门id和其最低工资。
    分析:
    第一步:查询50号部门的最低工资;
    select min(salary) from employees where department_id = 50;
    第二步:查询每个部门的最低工资。
    select department_id,min(salary) from employees group by department_id;
    第三步:从第二步结果集中筛选出min(salary) > 第一步结果集;
    select department_id,min(salary) from employees group by department_id having min(salary) > (select min(salary) from employees where department_id = 50);

三、非法使用标量子查询

  1. 例如>,< ,>= ,<=,=,<>操作符后面跟的是标量子查询,也就是单行单列,如果跟多行或多列,则会出错。
    错误示例:select department_id,min(salary) from employees group by department_id having min(salary) > (select salary from employees where department_id = 50);
    分析:select salary from employees where department_id = 50;子查询这个结果返回的是多行,所以会报错。

四、使用列子查询(多行子查询,一列多行)

  1. 返回location_id是1400或1700的部门中的所有员工姓名。
    分析:
    第一步:查询location_id是1400或1700的部门编号;
    select distinct department_id from departments where location_id in (1400,1700); // 返回单列多行
    第二步:查询员工姓名,要求部门号是第一步结果集中的任意一个;
    select last_name, department_id from employees where department_id in (select department_id from departments where location_id in (1400,1700));

  2. 返回其工种中比job_id为IT_PROG工种任一工资低的员工的员工号、姓名、job_id和salary。
    分析:
    第一步:查询job_id为IT_PROG部门的工资。
    select distinct salary from employees where job_id ='IT_PROG';
    第二步:查询其他工种工资比第一步中的工资任意低的员工的员工号、姓名、job_id和salary。
    select employee_id, last_name, job_id, salary from employees where salary < any(select distinct salary from employees where job_id ='IT_PROG') and job_id <> "IT_PROG";

    select employee_id, last_name, job_id, salary from employees where salary < (select max(salary) from employees where job_id ='IT_PROG') and job_id <> "IT_PROG";

  3. 返回其工种中比job_id为IT_PROG工种所有的工资都低的员工的员工号、姓名、job_id和salary。
    select employee_id, last_name, job_id, salary from employees where salary < all(select salary from employees where job_id ='IT_PROG') and job_id <> "IT_PROG";

    select employee_id, last_name, job_id, salary from employees where salary < (select min(salary) from employees where job_id ='IT_PROG') and job_id <> "IT_PROG";

五、使用行子查询(单行多列)

  1. 查询员工编号最小并且工资最高的员工信息。
    分析:
    第一步:查询员工最小编号;
    select min(employee_id) from employees;
    第二步:查询员工最高工资;
    select max(salary) from employees;
    第三步:查询满足前两个条件的员工信息;
    select * from employees where employee_id = (select min(employee_id) from employees) and salary = (select max(salary) from employees);
    使用行子查询写法:
    select * from employees where (employee_id, salary) = (select min(employee_id), max(salary) from employees);

子查询在select后面使用

  1. 查询每个部门的员工个数。
    select d.*, (select count(*) from employees e where e.department_id = d.department_id) as 员工个数 from departments d;
  2. 总结:select后面使用仅仅支持标量子查询结果,也就是返回的是一行一列,不然报错。

子查询在from后面使用

  1. 查询每个部门的平均工资的工资等级。
    分析:将子查询结果充当一张表,要求必须起别名;
    第一步:查询每个部门的平均工资;
    select avg(salary),department_id from employees group by department_id;
    第二步:连接第一步的结果集和job_grades表,筛选条件平均工资在lowest_sal和highest_sal之间;
    select avg_dep.*, g.grade_level from (select avg(salary) as avg_salary,department_id from employees group by department_id) avg_dep inner join job_grades g on avg_dep.avg_salary between g.lowerst_sal and highest_sal;

子查询在exists后面使用(相关子查询)

  1. 语法。
    select exists(select employee_id from employees);
    表示子查询的结果是否存在,存在则结果为1,不存在则为0。

  2. 查询有员工的部门名。
    select department_name from departments d where exists(select * from employees e where d.department_id = e.department_id);

    select department_name from departments d where d. department_id in (select department_id from employees);

子查询经典案例

一、 查询工资最低的员工名称和工资。

  1. 先查询最低工资,结果集为单行单列,标量子查
    select min(salary) from employees;
  2. 将第一步最低工资的结果集为查询条件,查询员工信息。
    select last_name, salary from employees where salary = (select min(salary) from employees);

二、 查询平均工资最低的部门信息。

  1. 先查询最低平均工资的部门ID。
    select department_id from employees group by department_id order by avg(salary) limit 1;
  2. 再查询平均工资等于1的部门信息。
    select * from departments where department_id = (select department_id from employees group by department_id order by avg(salary) limit 1);

三、 查询平均工资最低的部门信息和平均工资。

  1. 先查询最低平均工资的部门ID和平均工资。
    select avg(salary) ag_sal,department_id from employees group by department_id order by avg(salary) limit 1;
  2. 使用表子查询,再把1的结果集当成一张虚拟表,跟部门表连接,将平均工资当成虚拟表的虚拟字段,查询出来。
    select d.*, ag_sal from departments d inner join (select avg(salary) ag_sal, department_id from employees group by department_id order by avg(salary) limit 1) avg_de on d.department_id = avg_de.department_id;

四、 查询平均工资最高的job信息。

  1. 先查询最高的平均工资的job_id。
    select job_id from employees group by job_id order by avg(salary) desc limit 1;
  2. 再根据1中的结果查询job信息。
    select * from jobs where job_id = (select job_id from employees group by job_id order by avg(salary) desc limit 1);

五、 查询部门平均工资高于公司平均工资的部门有哪些。

  1. 先查询部门平均工资。
    select avg(salary), department_id from employees group by department_id;
  2. 再查询公司平均工资。
    select avg(salary) from employees;
  3. 由1筛选2,查出部门平均工资比公司平均工资高的部门。
    select avg(salary), department_id from employees group by department_id having avg(salary) > (select avg(salary) from employees);

六、 查询出部门中所有manager的详细信息。

  1. 先查询所有的manager的manager_id,并去掉重复的。
    select distinct manager_id from employees;
  2. 再查询员工id等于1中结果集的信息。
    select * from employees where employee_id in (select distinct manager_id from employees);

七、查询平均工资最高的部门的manager的信息。

  1. 先查询平均工资最高的部门编号;
    select department_id from employees group by department_id order by avg(salary) desc limit 1;
  2. 再查询部门编号=1结果集的manager的信息;
    select last_name from employees e inner join departments d on d.manager_id = e.employee_id where d.department_id = (select department_id from employees group by department_id order by avg(salary) desc limit 1);

这篇关于MySql数据库查询(DQL)语言—子查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1005616

相关文章

SQL中redo log 刷⼊磁盘的常见方法

《SQL中redolog刷⼊磁盘的常见方法》本文主要介绍了SQL中redolog刷⼊磁盘的常见方法,将redolog刷入磁盘的方法确保了数据的持久性和一致性,下面就来具体介绍一下,感兴趣的可以了解... 目录Redo Log 刷入磁盘的方法Redo Log 刷入磁盘的过程代码示例(伪代码)在数据库系统中,r

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

mysql中的group by高级用法

《mysql中的groupby高级用法》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,下面给大家介绍mysql中的groupby用法... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT

Mysql如何解决死锁问题

《Mysql如何解决死锁问题》:本文主要介绍Mysql如何解决死锁问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录【一】mysql中锁分类和加锁情况【1】按锁的粒度分类全局锁表级锁行级锁【2】按锁的模式分类【二】加锁方式的影响因素【三】Mysql的死锁情况【1

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

数据库面试必备之MySQL中的乐观锁与悲观锁

《数据库面试必备之MySQL中的乐观锁与悲观锁》:本文主要介绍数据库面试必备之MySQL中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/