当前位置:网站首页>MySQL | Subqueries
MySQL | Subqueries
2022-07-30 10:15:00 【Begonia_cat】
子查询
子查询指一个查询语句嵌套在另一个查询语句内部的查询.
The outer query is also called the main query,Inner queries are also called subqueries.
子查询分类
- Sort by the results of the subquery
- The query result is one:
单行子查询
; - There are multiple query results:
多行子查询
.
- The query result is one:
- Sort by the number of times the subquery was executed
- 子查询只执行一次,The execution result is executed as a condition of the main query:
不相关子查询
- The subquery is executed multiple times in a loop,依赖外部查询,To use an external table:
相关子查询
- 子查询只执行一次,The execution result is executed as a condition of the main query:
OR
(或) 与 IN
效果一样
WHERE A=1 OR A=2
IS EQUAL TO
WHERE A IN (1, 2)
推荐IN
题目:查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT department_id, MIN(salary)
FROM employee
GROUP BY department
HAVING MIN(salary) > (SELECT MIN(salary)
FROM employee
WHERE department_id = 50)
CASE
题目:显示员工的employee_id,last_name和location.其中,若员工department_id与location_id为1800
的department_id相同,则location为’Canada’,其余则为’USA’.
SELECT employee_id, last_name, (CASE department_id
WHEN (SELECT department_id
FROM employee
WHERE location_id=1800),
THEN location='Canana'
ELSE location='USA' END) location
FROM employee
ANY
题目:返回其它job_id中比job_id为‘IT_PROG’部门任一
工资低的员工的员工号、姓名、job_id 以及salary
SELECT employee_id, name, job_id, salary
FROM employees
WHERE job_id <> 'IT_PROG'
AND salary < ANY(SELECT salary
FROM employees
HAVING job_id='IT_PROG');
MIN
与 <=ALL
效果一样
查询平均工资最低的部门id
Mysql中,聚合函数不可以嵌套使用.oracle可以.
MIN(AVG(SALARY))
在mysqlkind of yes.
SELECT department_id
FROM employees
GROUP BY department
HAVING AVG(salary) <= ALL(SELECT AVG(salary) avg_salary
FROM employees
GROUP BY department)
相关子查询
如果存在,直接返回,不再查找.
如果不存在,继续查找,until found or all found.
EXIST
:题目字眼“存在
”NOT EXIST
:题目字眼“不存在
”
自连接:DISTINCT + 自连接
自连接优于
子查询,Self-connection is faster to process!
边栏推荐
- 再有人问你分布式事务,把这篇扔给他
- The use of qsort function and its analog implementation
- By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)
- leetcode 剑指 Offer 10- I. 斐波那契数列
- 2022年顶会accepted papers list
- 线程池方式开启线程--submit()和execute()的区别
- leetcode 剑指 Offer 46. 把数字翻译成字符串
- 水电表预付费系统
- leetcode 剑指 Offer 22. 链表中倒数第k个节点
- leetcode 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
猜你喜欢
随机推荐
By building a sequence table - teach you to calculate time complexity and space complexity (including recursion)
Flink_CDC搭建及简单使用
Re21: Read the paper MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
leetcode 剑指 Offer 46. 把数字翻译成字符串
Meikle Studio - see the actual combat notes of Hongmeng equipment development five - drive subsystem development
Practical Walkthrough | Calculate Daily Average Date or Time Interval in MySQL
Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
自适应控制——仿真实验一 用李雅普诺夫稳定性理论设计自适应规律
flowable工作流所有业务概念
Matplotlib--绘图标记
Shell系统学习之函数
leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
学习笔记10--局部轨迹生成主要方法
新一代开源免费的终端工具,太酷了
BERT预训练模型系列总结
多线程保证单个线程开启事务并生效的方案
105. 从前序与中序遍历序列构造二叉树(视频讲解!!)
The use of qsort function and its analog implementation
阿里云OSS对象存储
快解析结合任我行crm