美文网首页
mysql-子表查询

mysql-子表查询

作者: 戏之地 | 来源:发表于2017-02-23 18:57 被阅读269次
    • 子查询是将一个查询语句嵌套在另一个查询语句之中。

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

    • 子查询中可以包含: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-子表查询

          本文链接:https://www.haomeiwen.com/subject/icgbwttx.html