当前位置:网站首页>MySQL - subquery - column subquery (multi row subquery)
MySQL - subquery - column subquery (multi row subquery)
2022-07-25 22:05:00 【Sauerkraut】
Column query ( Multi line sub query )
The sub query returns data with multiple rows and single column , It's a list of data . Multi row subqueries are also called set comparison subqueries
When using multi row subqueries, you need to use multi row comparison operators :
| The operator | meaning |
| IN | Equal to any one of the list , Found in a set is established ,IN and =ANY It's the same |
| ANY | It needs to be used with the single line comparison operator (>、<、=、<>...), Compare with any value in the sub query results , Once established, it will be established |
| ALL | It needs to be used with the single line comparison operator (>、<、=、<>...), Compare with all the values returned by the subquery , Only when all conditions are established at the same time |
| SOME | It's actually ANY Another name for , The functions and ANY identical , So we use ANY |
IN The operator
MySQL --- Database query - Conditions of the query IN Use
IN Operator is used to determine whether the value of the expression is in the given list ; If it is , The return value is 1, Otherwise, the return value is 0.
NOT IN Function and IN On the contrary ,NOT IN Used to determine whether the value of the expression does not exist in the given list ; If not , The return value is 1, Otherwise, the return value is 0.
Find out the employee information with the same minimum wage in each department
Group by Department , Count the minimum wage of each department
Find out the employee information according to the minimum wage


Report errors , The subquery returns at most one row , But the requirements need to realize multiple lines , You need to equal 5000 Of 、 be equal to 800 Of 、 be equal to 950 Of 、 be equal to 1300 Salary output 
Use IN The operator , When the salary is in this set , Output the corresponding employees
-- Find out the information of each employee
-- SELECT * FROM emp WHERE sal=();
-- After getting the minimum wage of the whole department, group by department number
SELECT MIN(sal) FROM emp GROUP BY deptno;
-- Find out the employee information with the same minimum wage in each department
SELECT * FROM emp WHERE sal IN(SELECT MIN(sal) FROM emp GROUP BY deptno);
Number the Department NULL Remove the employee information of
-- Find out the information of each employee
-- SELECT * FROM emp WHERE sal=();
-- After getting the minimum wage of the whole department, group by department number
SELECT MIN(sal) FROM emp GROUP BY deptno;
-- Find out the employee information with the same minimum wage in each department
SELECT * FROM emp WHERE sal IN(SELECT MIN(sal) FROM emp GROUP BY deptno HAVING deptno IS NOT NULL);
Find out the employee information that is not the same as the minimum wage in each department
SELECT * FROM emp WHERE sal NOT IN(SELECT MIN(sal) FROM emp GROUP BY deptno HAVING deptno IS NOT NULL);
ANY The operator
ANY The keyword is a MySQL Operator , If the sub query criteria ANY Compared to TRUE, Then it will return Boolean value TRUE
Query employee information whose salary is higher than any management salary
Find out the salary of each management
Compare the salary of each employee with that of each manager
-- Find out the information of each employee
-- SELECT * FROM emp WHERE sal=();
-- Get all the management salaries
SELECT sal FROM emp WHERE job='MANAGER';
-- Query the information of employees whose salary is higher than any management salary 
Report errors , The subquery should return at most one row , Use at most one value for comparison , A value cannot be compared with multiple values , I don't know whether it's with 2975 Compare 、 Or with 2850 Compare 、 Or with 2450 Compare

-- Find out the information of each employee
-- SELECT * FROM emp WHERE sal=();
SELECT sal FROM emp WHERE job='MANAGER';
-- Query the information of employees whose salary is higher than any management salary
SELECT * FROM emp WHERE sal>=ANY(SELECT sal FROM emp WHERE job='MANAGER');
ALL The operator
ALL The keyword is a MySQL Operator , If the sub query criteria ALL Compared to TRUE, Then it will Returns a Boolean value TRUE
The case is the same as ANY The operator
There is no employee whose salary is equal to 2975、 be equal to 2850、 be equal to 2450
-- Find out the information of each employee
-- SELECT * FROM emp WHERE sal=();
SELECT sal FROM emp WHERE job='MANAGER';
-- Query the information of all employees whose salary is equal to the management salary
SELECT * FROM emp WHERE sal=ALL(SELECT sal FROM emp WHERE job='MANAGER');

summary
IN Judge whether it is in the set , Return true in the set , Do not return false in the set
ANY Judge whether it is in the set ( and IN equally )
>ANY Greater than the smallest one in the set
<ANY Less than the one with the largest value in the set
<>ANY Not equal to any one in the set
= ALL Equal to every value in the set
> ALL Larger than the largest value in the set
< ALL Smaller than the smallest value in the set
<> ALL Not equal to every value in the set
边栏推荐
- Excuse me, how to deal with repeated consumption of MySQL data
- The technical aspects of ByteDance are all over, but the result is still brushed. Ask HR why...
- 2 lines of code to generate a solid desktop background
- EL表达式改进JSP
- TS:typora代码片段缩进显示异常(已解决)-2022.7.24
- Bitcoin.com:usdd represents a truly decentralized stable currency
- Golang: MVC models
- 字节跳动技术面都过了,结果还是被刷了,问HR原因竟是。。。
- Share | intelligent fire emergency management platform solution (PDF attached)
- jsp九大内置对象
猜你喜欢

Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (judgment question) June 2022

Redis基础2(笔记)

核电站在席卷欧洲的热浪中努力保持安全工作

数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir

Redis foundation 2 (notes)

信息安全建设原则指导

Wet- a good choice for people with English difficulties - console translation
![[Fantan] how to design a test platform?](/img/54/5aca54c0e66f8a7c1c3215b8f06613.png)
[Fantan] how to design a test platform?
![[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_ READ_ File: read file failed problem solving
![[leetcode ladder] linked list · 021 merge two ordered linked lists](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode ladder] linked list · 021 merge two ordered linked lists
随机推荐
golang : MVC之models
【测开方法论】测开平台pk心得-抉择
New maixhub deployment (v831 and k210)
Special class design
What is redis? Briefly describe its advantages and disadvantages
[hand torn STL] unordered_ set、unordered_ Map (encapsulated with hash table)
【汇编语言01】基础知识
El expression improves JSP
TS:typora代码片段缩进显示异常(已解决)-2022.7.24
Performance debugging -- chrome performance
What are the application characteristics of NTU general database gbase Bi?
【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)
6-17漏洞利用-反序列化远程命令执行漏洞
How to quickly build a picture server [easy to understand]
[51Nod1676 无向图同构]无向图哈希[通俗易懂]
EL表达式改进JSP
[fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
zigbee开发板(nxpzigbee开发)
【饭谈】软件测试薪资层次和分段(修仙)
JSP novice