当前位置:网站首页>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');

边栏推荐
- Oauth2==sso three protocols. Oauth2 four modes
- UNI-APP_ Playback and pause of background music of applet or H5 page
- Selenium basic knowledge multi window processing
- 25. Message subscription and publishing - PubSub JS
- requests-爬虫实现一个简易网页采集器
- Arduino's super power-saving sleep mode has worked with one 18650 battery for 17 years
- HCIP第七天
- One click Copy and import of web interface data into postman
- Flinksql UDF custom data source
- Eight part essay on software testing
猜你喜欢

Harbor2.2 quick check of user role permissions

Mutual implementation of stack and queue (c)

爬虫学习-概述

JMeter stress test index interpretation

Selenium basic knowledge debugging method

The solution of unable to import custom library in pycharm
![2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <](/img/f4/ba2706e93f042dd8b110fac0d873c8.png)
2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <

【云原生】MySql索引分析及查询优化

Influxdb unauthorized access & CouchDB permission bypass

requests-爬取页面源码数据
随机推荐
基于VSCode聊聊编译器那些事儿
MySQL --- 子查询 - 标量子查询
Deep learning two or three things - review those classical convolutional neural networks
Requests crawler multi page crawling to KFC restaurant location
Harbor2.2 quick check of user role permissions
China trichlorosilane Market Forecast and Strategic Research Report (2022 Edition)
Unable to auto assemble, bean of type "redistemplate" not found
[Huawei] Huawei machine test question-105
Amber tutorial A17 learning - concept
GBK code in idea is converted to UTF-8 format ctrl+c+v one second solution perfect solution for single code file escape
Do you know the use of string?
Function analysis of e-commerce website development and construction
Selenium basic knowledge automatically login Baidu online disk
Laplace distribution
Super simple countdown code writing
Mutual implementation of stack and queue (c)
【信息系统项目管理师】第七章 复盘成本管理知识架构
[sklearn] RF cross validation out of bag data parameter learning curve grid search
Selenium basic knowledge paging processing
Selenium basic knowledge automatic login QQ space