当前位置: 首页 > news >正文

【MySQL】P9 多表查询(3) - 子查询

子查询

  • 子查询
    • 基本概念(公式)
    • 子查询分类
  • 按照结果分类
    • 标量 子查询
    • 列 子查询
    • 行 子查询
    • 表 子查询


子查询

基本概念(公式)

SQL查询语句中嵌套Select语句,称为嵌套查询,亦称为子查询;

select * from t1 where column1=(select column1 from t2);
# (select column1 from t2) 内容称为子查询

子查询分类

根据子查询结果不同,可以分为:

  1. 标量子查询(子查询结果为单个值);
  2. 列子查询(子查询结果为一列);
  3. 行子查询(子查询结果为一行);
  4. 表子查询(子查询结果为多行多列);

根据子查询位置,可以分为:

  1. where 子查询
  2. from 子查询
  3. select 子查询

按照结果分类

标量 子查询

标量子查询:子查询结果为单个值;

e.g.1e.g.1e.g.1 查询销售部的员工信息:

# 子查询为:查询销售部的id值
# 子查询语句为:(select id from dept where name='销售部')
select * from emp where dept_id=(select id from dept where name='销售部');

e.g.2e.g.2e.g.2 查询在 老徐 入职之后的员工信息

# 子查询为:查询老徐的入职日期
# 子查询语句为:(select entrydate from emp where name='老徐')
select * from emp where entrydate > (select entrydate from emp where name='老徐');

列 子查询

列子查询:子查询返回的结果是一列;

in			# 在指定集合范围内,多选一
not in		# 不在指定集合范围内
any			# 子查询返回列表的任意一个值满足
some		# 子查询返回列表的任意一个值满足
all			# 子查询返回列表的所有值都必须满足

e.g.1e.g.1e.g.1 查询销售部和市场部所有员工信息

# 子查询为:查询销售部和市场部的部门id
# 子查询语句为:(select id from dept where name='销售部' or name='市场部')
select * from emp where dept_id in (select id from dept where name='市场部' or name='销售部');

e.g.2e.g.2e.g.2 查询比财务部所有人工资都高的员工信息

# 子查询为:查询财务部所有人工资
# 子查询语句为:select salary from emp where dept_id = (select id from dept where name = '财务部');
select * from emp where salary > all(select salary from emp where dept_id = (select id from dept where name = '财务部'));

e.g.3e.g.3e.g.3 查询比研发部任意一个员工工资高的员工信息

# 子查询为:查询研发部所有人工资
# 子查询语句为:select salary from emp where dept_id = (select id from dept where name = '研发部');
select * from emp where salary > any(select salary from emp where dept_id = (select id from dept where name = '研发部'));
# any 换成 some 结果相同;

行 子查询

行子查询:子查询返回的结果是一行,多列;

e.g.e.g.e.g. 查询与 老徐 的薪资以及直属领导相同的员工信息

# 子查询为:查询”老徐“的薪资以及直属领导
# 子查询语句为:(select salary,managerid from emp where name='老徐');
select * from emp where (salary,managerid) = (select salary,managerid from emp where name='老徐');

表 子查询

表子查询:子查询返回结果是多行多列;

e.g.1e.g.1e.g.1 查询与 老徐,老张 的职位和薪资相同的员工信息

# 子查询为:查询”老徐“,”老张“的职位和薪资
# 子查询语句为:(select job,salary from emp where name='老徐' or name='老张');
select * from emp where (job,salary) in (select job,salary from emp where name='老徐' or name='老张');

e.g.2e.g.2e.g.2 查询入职日期是2006-01-01之后的员工信息,以及其部门信息;

# 子查询为:查询入职日期在2006-01-01之后的员工信息
# 子查询语句为:(select * from emp where entrydate > '2006-01-01');
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;

http://www.lryc.cn/news/34584.html

相关文章:

  • SpringMVC中的拦截器不生效的问题解决以及衍生出的WebMvcConfigurationSupport继承问题思考
  • 【量化交易笔记】3.实现数据库保存数据
  • [数据结构]:15-堆排序(顺序表指针实现形式)(C语言实现)
  • 蓝桥 卷“兔”来袭编程竞赛专场-02破解曾公亮密码 题解
  • CSS定位
  • python sympy库
  • 达梦数据库统计信息的导出导入
  • 信息系统基本知识(六)
  • <C++>智能指针
  • 1.分析vmlinux可执行文件是如何生成的? 2.整理内核编译流程:uImage/zImage/Image/vmlinx之间关系
  • 数据结构4——线性表3:线性表的链式结构
  • weblogic 忘记密码重置密码
  • 安卓开发之动态设置网络访问地址
  • 深度学习模型训练工作汇报(3.8)
  • 【ns-3】添加nr(5G-LENA)模块
  • (枚举)(模拟)(前缀和)(数组模拟哈希)(可二分)1236. 递增三元组
  • mysql五种索引类型(实操版本)
  • 微服务进阶之 SpringCloud Alibaba
  • 前端性能优化笔记2 第二章 度量
  • 关于new和delete的一些思考,为什么不能在析构函数中调用delete释放对象的内存空间,new和delete的原理
  • 一场以数字技术深度影响和改造传统实业的新风口,正在开启
  • 【LeetCode】13. 罗马数字转整数
  • 2023/3/8集合之TreeSet HashSet简介 不含代码
  • 【面试1v1实景模拟】面试中常见的Java关键字详解
  • MySQL8.0.16存储过程比5.7.22性能大幅下降
  • 基于MATLAB的无线信道的传播与衰落(附完整代码与分析)
  • SDX62如何查看Kernel版本和Operating System Version Patch Level
  • 001+limou+HTML——(1)HTML入门知识
  • 使用Arduino Uno构建一个巡线机器人
  • 【C++】类和对象(收尾)