当前位置:网站首页>Advanced learning of MySQL -- basics -- multi table query -- subquery

Advanced learning of MySQL -- basics -- multi table query -- subquery

2022-07-07 00:34:00 Magic pig 9898

Concept

SQL Nested in statement select sentence
The external statement of a subquery can be insert/update/delete/select Any one of .
That is said , DML and DQL You can subquery

Depending on the results of the subquery , It is divided into

Scalar subquery

The commonly used operator is : > < >= <= <>
The result is a single value
for example :
Query the information of all employees in the sales department
select * from emp where dept_id =
(select id from dept where name = ' The sales department '); This is scalar subquery

Column query

The result is a column ( Are some values of a field , In other words , Multiple scalars )
Let's first look at a few operators

The operator explain
in Within the specified set , A commonplace
not in Not within the specified range
any/some The subquery returns to the list , Any one can be satisfied
all The sub query return list must all meet

for example :
Query all employee information of sales department and marketing department
select * from emp where dept_id in
(select id from dept where name = ' The sales department ' or name = ' The Marketing Department '); This is the column subquery

Another example is :
Check the information of employees whose salaries are higher than those of everyone in the finance department
select * from emp where salary > all
(select salary from emp where dept_id =
(select id from dept where name = ' Finance Department ') );

Another example is :
Query the information of employees with higher wages than any one in the R & D department
select * from emp where salary > some
(select salary from emp where dept_id =
(select id from dept where name = ' R & D department ') );

Line sub query

The result is one line ( Multiple fields , In other words , Single scalar Group )
Common operators := <> in not in
for example :
Query the same employee information as Xiaobai's salary and the leader
select * from emp where (salary , managerid) =
(select salary , managerid from emp where name = ' The small white ');

Table sub query

The result is multiple rows and columns ( There is more than one scalar Group )
Common operators : in
for example :
Query and Xiaobai , Xiaohei's position and the information of employees with the same salary
select * from emp where (job, salary) in
(select job , salary from emp where name = ' The small white ' or name = ' Little black ');

Another example is :
The entry time is ’2001-01-06’ Later employee information , And its department information
select e.* , d.* from
(select * from emp where entrydate > '2001-01-06' ) e
left outer join
dept d
on e.dept_id = d.id ;

According to the location of subquery , It is divided into

select after

from after

where after

原网站

版权声明
本文为[Magic pig 9898]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130957051979.html