Subquery of sql statement

Sub-query is also called nested query, summarize the various operations and processing of sub-query, in order to facilitate memory and skilled use.

Concept: What is a subquery?

A subquery is to nest a query statement in another query statement. The query result of the inner statement can be used as the condition of the outer query statement. This article focuses on the situation after the where or having keywords. Of course, not only Limited to this situation.

Subqueries are divided into the following three situations (only three of the more used ones are summarized here)

1. Column subquery (multi-row subquery, that is, the result set of the subquery is multiple rows and single column), the position is placed after where and having.

Contains the following common situations:

1. Use [not] in subquery

2. Subqueries using comparison operators (=, >, >=, <=, <>, !=, <=>)

3. Subquery using [not] exists

4. Use any some or all sub-queries, as shown in the figure on the right:

Operating float meaning
IN/NOT IN Equal to any one in the list
ANY|SOME Compare with a value returned by the subquery
ALL Compare with all the values ​​returned by the subquery
Operator|keyword any some all
>     >= Minimum Minimum Max
<     <= Max Max Minimum
= Any value Any value   
<>   !=     Any value
(仅示例)

select id ,username from student where score >=any(select level from scholarship);大于等于任意一个,即取最小值

select id ,username from student where score >=some(select level from scholarship)同上

select id ,username from student where score >=all(select level from scholarship)大于等于所有的,就是大于等于里面最大的那个


其他情况参考这个示例

Two, scalar quantum query

The sub-result set of the scalar sub-query is a single row and single column, and the sub-query conditions are generally placed after where or having, and should be placed in () without adding ";".

To illustrate:

select * from employees 

where salary >(

    select  salary  from employees where 
    last_name='Abel'

);
select department_id ,min(salary) from employees 

group by department_id 

having min(salary) >( 

select min(salay)

from employees where department_id =50
);

 Three, row sub-query (the result set is one row and multiple columns or multiple rows and multiple columns, less usage, not recommended)

for example:

select * from employees 

where (employee_id ,salary )=(

select min(employee_id),max(salary)

from employees

);


当然用之前的方法也是可以解决的:

select * from empioyees

where employee_id=(

select min(employee_id) from employees

)

and 

salary=(

select max(salary) from employees

);

Of course, there are some other uses of subqueries, such as the following when creating a table.

Write query results to the data table

insert table  [column_name,.......] select column_name,....... from table;

Write the query results into the data table while creating the data table

create table if not exists table_name [create_definition,......] select_statement

ps: It should be noted here that the fields of the newly created table must be consistent with the query fields before the data can be generated according to the corresponding fields. If they are inconsistent, new fields will be generated on the original basis.

create table test2 (id tinyint unsigned auto_increment key ,num tinyint unsigned ) select id ,level from scholarship ;

+------+----+-------+
| num  | id | level |
+------+----+-------+
| NULL |  1 |    90 |
| NULL |  2 |    80 |
| NULL |  3 |    70 |
+------+----+-------+

这样就多出了一个level字段或者说num字段没有用上,如果我们把num字段改成level就能生成合适的表格内容了。
+----+-------+
| id | level |
+----+-------+
|  1 |    90 |
|  2 |    80 |
|  3 |    70 |
+----+-------+

In addition, some sub-query statements are placed under select. Give an example to illustrate:

select d.*,(

select count(*)

from employees e 

where e.department=d.department

)

from departments d;


select  (

select department_name from departments d

join employees e

on d.department_id =e.department_id

) from departments d;

Another part is placed after from. Generally, from is followed by a table. This is equivalent to treating the result set of the subquery as a table. It is no longer an example. If you are interested, you can Baidu.

Guess you like

Origin blog.csdn.net/weixin_42575020/article/details/113258897