MYSQL database foundation - how to operate MySQL subquery?

insert image description here

foreword

Starting today, this series of articles will take your friends to learn database technology. Database technology is an indispensable part of knowledge content in Java development. It is also a very important technology. This series of tutorials comprehensively explains the database system from the shallower to the deeper. It is very suitable for small partners with zero foundation to learn.


The full text is about [1258] words , no nonsense, just pure dry goods that allow you to learn techniques and understand principles! This article has a wealth of cases and pictures, so that you can better understand and use the technical concepts in the article, and can bring you enough enlightening thinking...

1. Subquery

What is a subquery? Brother Jian is here to tell you all about it. The so-called sub-query is to use a query result as a judgment condition or as a virtual table to perform another query on the basis of this result.

1. Subquery (as a conditional judgment)

The following is to use the query result as the judgment condition of another query.

grammar :SELECT 列名 FROM 表名 Where 条件 (子查询结果)

For example: Query other students who are the same age as Brother Jian.

#1.先查询到健哥的年龄
select Sage from student where Sname='健哥'; #年龄是12

#2.查询年龄等于健哥年龄的同学
select * from student where Sage=12;

#3.将 1、2 两条语句整合
select * from student where Sage=(select Sage from student where Sname='健哥');

Note: The result of the subquery "one row and one column" is used as the condition of the external query, and the result of one row and one column obtained by the second query subquery can be used as the equivalence judgment condition or inequality condition judgment of the external query.

2. Subquery (as an enumerated query condition)

Using a subquery as an enumeration query condition is to use the result set of a query result (single column and multiple rows) as an enumeration query condition for secondary query.

grammar:SELECT 列名 FROM 表名 Where 列名 in(子查询结果);

Take a chestnut: query the information of students who are the same age as Brother Jian and Brother Xu.

#思路:
#1. 先查询健哥和旭哥的年龄(多行单列)
select Sage from student where Sname in('健哥', '旭哥'); #年龄是12和23

#2. 再查询年龄为12和23的学员信息
select * from student where Sage in(12, 23); 

#3.SQL:合并
select * from student where Sage in(select Sage from student where Sname in('健哥', '旭哥')); 

Use the result of the subquery "multiple rows and one column" as the enumerated query condition of the outer query to do the second query.

3. Subquery (as a table)

Brother Jian is knocking on the blackboard here! Very important and used frequently!

The following subquery treats the result of a query as a virtual table, and then performs a query based on the result of this table.

grammar:SELECT 列名 FROM (子查询的结果集) WHERE 条件;

Take a chestnut: query the information of the oldest 5 students.

#思路:
#1. 先对学生年龄排序查询(排序后的临时表)
select * from student order by Sage desc;

#2. 再查询临时表中前5行学生信息
select *
from (临时表) 
limit 0,5;

#SQL:合并
select * from (
		select * from student order by Sage desc
) as temp limit 0,4;

Use the result of the subquery "multiple rows and multiple columns" as a table of the external query, and do the second query.

Note: The subquery is used as a temporary table, so give it a temporary table name.


2. Conclusion

Finally, here is a summary of the core points of this article:

  1. There are three usage scenarios for subqueries: the subquery result is used as a judgment condition, the subquery result is used as an enumeration condition, and the subquery result is used as a virtual table for secondary query.

  2. The above three usage methods, you guys should be proficient in mastering them.

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130519436