当前位置:网站首页>MYSQL08_ Overview of sub query, single row, multi row, related sub query
MYSQL08_ Overview of sub query, single row, multi row, related sub query
2022-07-21 19:16:00 【What you get is a surprise】
List of articles
①. Overview of subqueries
①. Subquery refers to a query that is nested within another query statement by one query statement , This feature comes from MySQL 4.1 Start introducing
②. Subquery ( Internal query ) In the main query ( External query ) The previous execution completed
③. The results of the sub query are by the main query ( External query ) Use
④. Sub query considerations :
- Subqueries should be enclosed in parentheses
- Place the subquery to the right of the comparison criteria
- Single line operators correspond to single line subqueries , Multiline operators correspond to multiline subqueries
# Whose pay ratio is Abel high
# Mode one :
SELECT salary FROM employees WHERE last_name = 'Abel';
SELECT last_name,salary FROM employees WHERE salary > 11000;
# Mode two : Self join
SELECT e2.last_name,e2.salary FROM employees e1,employees e2
WHERE
e1.last_name = 'Abel' AND e1.`salary` < e2.`salary`
# Mode three : Subquery
SELECT last_name,salary FROM employees
WHERE
salary > ( SELECT salary FROM employees WHERE last_name = 'Abel' );
- ⑤. We return one or more records according to the results of internal query , Sub query is divided into single line sub query 、 Multi line sub query
- ⑥. We check whether the query is executed more than once , Divide the subquery into relevant ( Or associated ) Subquery and unrelated ( Or unrelated ) Subquery
- The subquery queries the data results from the data table , If this data result is executed only once , The data result is then executed as a condition of the main query , Then such a subquery is called an unrelated subquery
- If the subquery needs to be executed multiple times , That is, in the form of circulation , Start with an external query , Every time a subquery is passed in to query , The results are then fed back to the outside world , This nested execution is called related subquery
②. Subquery - Single line sub query
①. Single line comparison operator
②. Subquery title - sql Exhibition
# (1). return job_id And 141 Same as employee No ,salary Than 143 The name of the employee with a large number of employees ,job_id And wages
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 );
# (2). Return to the lowest paid employees of the company last_name,job_id and salary
SELECT
last_name,
job_id,
salary
FROM
employees
WHERE
salary = ( SELECT MIN( salary ) FROM employees );
# (3). Query and 141 Number or 174 Of employee No manager_id and department_id Of the same other employees employee_id, manager_id,department_id
SELECT
employee_id,
manager_id,
department_id
FROM
employees
WHERE
manager_id IN ( SELECT manager_id FROM employees WHERE employee_id IN ( 174, 141 ) )
AND department_id IN ( SELECT department_id FROM employees WHERE employee_id IN ( 174, 141 ) )
AND employee_id NOT IN ( 174, 141 )
# (4). The minimum wage is greater than 50 The Department of minimum wage id And its minimum wage
SELECT
department_id,
MIN( salary )
FROM
employees
GROUP BY
department_id
HAVING
MIN( salary ) > ( SELECT MIN( salary ) FROM employees WHERE department_id = 50 );
# (5). Explicit employee employee_id,last_name and location. among , If employee department_id And location_id by 1800 Of department_id identical , be location by ’Canada’, The rest are ’USA
SELECT
employee_id,
last_name,
( CASE department_id WHEN ( SELECT department_id FROM departments WHERE location_id = 1800 ) THEN 'Canada' ELSE 'USA' END ) location
FROM
employees;
边栏推荐
- [QNX hypervisor 2.2 user manual]8.6 interrupt
- Spark RDD, application case of spark SQL
- 【QT】日志路径的字符编码转换正确方式
- Software testing interview question: what is the task of testers in the process of software development?
- mapbox-gl开发教程(十三):加载三维面图层(白模)
- 不想醒来因为好吃
- 软件测试面试题:测试人员在软件开发过程中的任务是什么?
- Jujube technology CEO's monthly DDC briefing (phase III) -- Introduction to new functions of DDC network and review of essence of Wenchang chain upgrade plan
- 软件测试面试题:你对测试最大的兴趣在哪里?为什么?
- 正则表达式
猜你喜欢
打造高性能应用,持续优化用户体验
FTP服務配置
手把手教你安装MySQL数据库
MYSQL06_sql99的7种JOIN操作、union all
Unity—Input类
PostgreSQL source code (64) query execution - sub module executor (2) data structure and execution process before execution
外设驱动库开发笔记44:DDC114 ADC驱动
Unity - input class
Alibaba cloud released the industry's first "best practice map of live video technology"!
Configuration du Service FTP
随机推荐
JMeter学习笔记003-JMeter之CSV参数化
liunx 批量杀死同名进程
论文翻译解读:Anytime Bottom-Up Rule Learning for Knowledge Graph Completion【AnyBURL】
Mapbox GL development tutorial (13): loading 3D surface layers (white mold)
软件测试面试题:黑盒测试和白盒测试是软件测试的两种基本方法,请分别说明各自的优点和缺点!
Tutorial (7.0) 03 Forticlient EMS configuration and Management * forticlient EMS * Fortinet network security expert NSE 5
MYSQL09_精讲数据库数据类型
论文翻译解读:Logmap:Logic-based and scalable ontology matching
[MCU simulation project] 4 × 4 matrix keyboard scanning
Software testing interview question: what is the purpose of test planning? What should be included in the test plan document? Which are the most important?
Synchronized lock range
Spark RDD, application case of spark SQL
【LeetCode】1260. 二维网格迁移
spark rdd,spark SQL的应用案例
软件测试面试题:您认为在测试人员同开发人员的沟通过程中,如何提高沟通的效率和改善沟通的效果?维持测试人员同开发团队中其他成员良好的人际关系的关键是什么?
什么是 GameFi?
Software testing interview question: what work is usually done in each stage of software testing? What are the outcome documents of each stage? What is included?
Clickhouse Fundamentals
NFT:数字所有权的核心
How easyexcel exports files in a project