mysql-子表查询

  • 子查询是将一个查询语句嵌套在另一个查询语句之中。

  • 子查询的结果可为外层查询提供一个范围

  • 子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字

  • 还可以包含比较运算符:= 、 !=、> 、<等

select * from employee
where dept_id IN
(select dept_id from department);

 select dept_id,dept_name from department
       where dept_id IN
      (select DISTINCT dept_id from employee where age>=25);

##子查询的EXISTS关键字

``` select * from employee
                WHERE EXISTS
              (SELECT dept_name from department where dept_id=205);

如果EXISTS后面非空,TRUE,否则为空,外层查询不会有值了

你可能感兴趣的:(mysql-子表查询)