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

边栏推荐
- 24.全局事件总线
- Introduction to C language
- MS SQL Server 2019 learning
- Install librosa using Tsinghua image
- Reptile learning - Overview
- Log in to the server using the fortress machine (springboard machine)
- 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] <
- Deep analysis of data storage in memory
- Starting from scratch C language intensive Part 3: Functions
- C语言文件操作
猜你喜欢

无法自动装配,未找到“RedisTemplate类型的Bean

Source code analysis of Nacos configuration center

Collection of linked list topics

Error when using PIP: pip is configured with locations that requires tls/ssl

Devops essay

Introduction to C language II. Functions
![[sklearn] RF cross validation out of bag data parameter learning curve grid search](/img/1f/936e4fe42ed97de2c3caa3f025744d.png)
[sklearn] RF cross validation out of bag data parameter learning curve grid search

Postman extracts the token parameter value in the response header and sets it as an environment variable, with code attached

Function analysis of e-commerce website development and construction

requests-爬虫实现一个简易网页采集器
随机推荐
Feature Selective Anchor-Free Module for Single-Shot Object Detection
Oauth2==sso three protocols. Oauth2 four modes
【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化
NFT概念究竟是怎么回事。。全面了解NFT市场、技术和案例
Advanced part of Nacos
Laplace distribution
Selenium basic knowledge debugging method
Example of dictionary
【Pytorch】conv2d torchvision.transforms
The difference between session and cookie
2022-07-23:给定N件物品,每个物品有重量(w[i])、有价值(v[i]), 只能最多选两件商品,重量不超过bag,返回价值最大能是多少? N <= 10^5, w[i] <= 10^5, v
2021-06-03 database query - sorting
Selenium basic knowledge multi window processing
UNI-APP_ Playback and pause of background music of applet or H5 page
App performance test case
MS SQL Server 2019 learning
Postman extracts the token parameter value in the response header and sets it as an environment variable, with code attached
Collection of sorting topics
Eight part essay on software testing
CentOS 7 install mysql5.6.37