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

边栏推荐
- 23. Component customization events
- Facing Tencent (actual combat) - Test Development - detailed explanation of interns (face experience)
- numpy.concatenate
- Influxdb unauthorized access & CouchDB permission bypass
- Introduction to C language II. Functions
- Selenium basics controls the scroll bar of the browser
- Advanced part of C language I. data storage
- Have you seen the interview questions of VR major? Trust me, it's absolutely useful
- Unable to auto assemble, bean of type "redistemplate" not found
- The solution of unable to import custom library in pycharm
猜你喜欢

【sklearn】tree.DecisionTreeClassifier

Simple Gateway - intranet server safely obtains external network data

Deep analysis of data storage in memory

Amber tutorial A17 learning - concept

Debug No3 multi texture overlay

Introduction to C language III Array 4. Operators

Advanced part of C language I. data storage

JMeter stress test index interpretation

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

About using the alignment function of VMD
随机推荐
Sword finger offer special assault edition day 8
Harbor2.2 quick check of user role permissions
Requests crawler implements a simple web page collector
Induction, generalization, deduction
Source code analysis of Nacos configuration center
Selenium use
Influxdb unauthorized access & CouchDB permission bypass
UNI-APP_ Playback and pause of background music of applet or H5 page
One click Copy and import of web interface data into postman
Collection of linked list topics
【sklearn】tree.DecisionTreeClassifier
Mutual implementation of stack and queue (c)
2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib
爬虫学习-概述
Appium use
Appium doctor command error pit - resolved
C language advanced part III. string functions and memory operation functions
HCIP第十天笔记
Advanced part of Nacos
Implement a queue with two stacks.