当前位置:网站首页>MySQL -- subquery scalar subquery
MySQL -- subquery scalar subquery
2022-07-24 07:46:00 【Sauerkraut】
Scalar subquery
MySQL --- Subquery - Sub query concept 、 standard 、 classification
The sub query returns the data of single row and single column , It's a value
Mainly used in WHERE clause
You need to use some operators Greater than >、 Less than <、 be equal to =、 It's not equal to <>、!=
Writing method of subquery
① Write from outside to inside
② Write from the inside out
Find the basic salary ratio ALLEN Low total employee information
Write from outside to inside
-- Find out all employee information
SELECT * FROM emp;
-- ALLEN The salary of
SELECT sal FROM emp WHERE ename='ALLEN';
-- Query out ([ Basic wage ratio ALLEN Low ] All employee information )
SELECT * FROM emp WHERE sal<(SELECT sal FROM emp WHERE ename='ALLEN');


Query the information of all employees whose basic salary is higher than the average salary of the company
-- Query the average salary of the company
SELECT AVG(sal) FROM emp;/*1988.3333*/
-- Query the information of all employees whose basic salary is higher than the average salary of the company
SELECT * FROM emp WHERE sal>1988.3333;
-- Inquire about [ The basic salary is higher than ( Average salary of the company ) All employee information ]
SELECT * FROM emp WHERE sal>(SELECT AVG(sal) FROM emp);Write from the inside out
MySQL --- Database query - The use of aggregate functions 、 Aggregate query 、 Group query



Find out and ALLEN In the same job , And the basic salary is higher than the employee number 7521 All employee information
-- Find out and ALLEN In the same job
SELECT * FROM emp
WHERE job=(SELECT job FROM emp WHERE ename='ALLEN')
-- And the basic salary is higher than the employee number 7521 All employee information
AND sal>(SELECT sal FROM emp WHERE empno=7521)
-- hold ALLEN Remove from query results
AND ename<>'ALLEN';


You can find that the query results contain ALLEN, Need to put ALLEN Remove from query results

Single line sub query
Subquery returns single row and multi column data , It's just a record
Query and SCOTT Information of employees who are engaged in the same job and have the same salary
-- Query and SCOTT Information of employees who are engaged in the same job and have the same salary
SELECT * FROM emp
WHERE job=(SELECT job FROM emp WHERE ename='SCOTT')
AND sal=(SELECT sal FROM emp WHERE ename='SCOTT')
AND ename<>'SCOTT';Use scalar subqueries

Use a single line subquery
-- Inquire about SCOTT Employee information
SELECT job,sal FROM emp WHERE ename='SCOTT';
-- Query and SCOTT Information of employees who are engaged in the same job and have the same salary
SELECT * FROM emp WHERE (job,sal)=(SELECT job,sal FROM emp WHERE ename='SCOTT');

The inquiry and employee number are 7566 Information of all employees who are engaged in the same work and lead the same
-- The inquiry and employee number are 7566 Information of all employees who are engaged in the same work and lead the same
SELECT * FROM emp WHERE
(job,mgr)=(SELECT job,mgr FROM emp WHERE empno=7566);

Query and ALLEN Information of all employees engaged in the same job and employed in the same year ( contain ALLEN)
Same number of columns 、 The same data type can be compared directly
-- Query and ALLEN Information of all employees engaged in the same job and employed in the same year ( contain ALLEN)
SELECT * FROM emp WHERE
(job,DATE_FORMAT(hiredate,'%Y'))=
(SELECT job,DATE_FORMAT(hiredate,'%Y') FROM emp WHERE ename='ALLEN');

边栏推荐
- Jenkins detailed deployment
- MS SQL Server 2019 learning
- Game three piece chess
- Selenium basic knowledge debugging method
- 23. Component customization events
- Fopen, fwrite, fseek, fTell, FREAD use demo
- JMeter stress test index interpretation
- C语言文件操作
- About the solution of thinking that you download torch as a GPU version, but the result is really a CPU version
- Feature Selective Anchor-Free Module for Single-Shot Object Detection
猜你喜欢

Jackson 解析 JSON 详细教程
![2022-07-23:给定N件物品,每个物品有重量(w[i])、有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v](/img/f4/ba2706e93f042dd8b110fac0d873c8.png)
2022-07-23:给定N件物品,每个物品有重量(w[i])、有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v

NFT概念究竟是怎么回事。。全面了解NFT市场、技术和案例

【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化

Vertex buffer and shader (the cherno + leranopongl) notes

MySQL 啥时候用表锁,啥时候用行锁?

MS SQL Server 2019 学习

One click Copy and import of web interface data into postman

Selenium basic knowledge debugging method

Sense dimension design responsive layout
随机推荐
Train-clean-100 dataset
Laplace distribution
The difference between session and cookie
Unable to auto assemble, bean of type "redistemplate" not found
Postman extracts the token parameter value in the response header and sets it as an environment variable, with code attached
Selenium basic knowledge automatic login QQ space
Advanced part of C language IV. detailed explanation of user-defined types
Selenium basic knowledge automatically login Baidu online disk
Devops essay
Jetson AgX Orin source change
Anaconda install pytorch
Debug No3 multi texture overlay
C language advanced part III. string functions and memory operation functions
Facing Tencent (actual combat) - Test Development - detailed explanation of interns (face experience)
requests-爬取页面源码数据
Detailed explanation of VAO
Collection of sorting topics
25. Message subscription and publishing - PubSub JS
C language to achieve mine sweeping game
Selenium basic knowledge automatic search