zoukankan      html  css  js  c++  java
  • mysql_子查询

    1:子查询是将一个查询语句嵌套在另一个查询语句中。
    2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
    3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
    4:还可以包含比较运算符:= 、 !=、> 、<等

    #查询平均年龄在25岁以上的部门名
    select id,name from department
    where id in (select dep_id from employee group by dep_id having avg(age) > 25);

    #查看技术部员工姓名
    select name from employee
    where dep_id in 
    (select id from department where name='技术');

    #查看不足1人的部门名(子查询得到的是有人的部门id)
    select name from department where id not in (select distinct dep_id from employee);

    #查询大于所有人平均年龄的员工名与年龄
    select name,age from emp where age > (select avg(age) from emp);

    带EXISTS关键字的子查询

    EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。
    而是返回一个真假值。True或False
    当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询

    #department表中存在dept_id=203,如果存中则返回true,否则返回false。
    select * from employee
    where exists
    (select id from department where id=200);

    不存在204则:

    select * from employee
    where exists
    (select id from department where id=204);

  • 相关阅读:
    在java中获取URL的域名或IP与端口
    解决notepad++64位没有plugin manager的问题
    统一认证需要解决的问题
    搭建Maven私服
    Update openssh7.9 on centos6
    python下载想听的有声书,让喜马拉雅收费,我是程序员!
    golang ---获取内存信息
    websocket学习
    go 读取BMP文件头二进制读取
    go 计算文件行
  • 原文地址:https://www.cnblogs.com/wangdianchao/p/12283456.html
Copyright © 2011-2022 走看看