• MySQL(子查询)


    子查询

    #1.查询平均工资最低的部门

    mysql中聚合函数是不能嵌套使用的

    所以,这样的代码是错误的,无法执行

    1. SELECT MIN(AVG(salary))
    2. FROM employees
    3. GROUP BY department_id
    • 正确的查询如下: 

    先查询到最低的部门平均工资

    • 方法在FROM中声明子查询(查询的部门平均工资生成新的表,然后再select ...from(新生成的表),即可对新生成的表进行查询) 
    1. SELECT MIN(avg_salary)
    2. FROM(
    3. SELECT AVG(salary) avg_salary #需要给AVG(salary)字段生成别名,因为不能在MIN里面再套AVG
    4. FROM employees
    5. GROUP BY department_id
    6. ) dep_avg_salary #新生成的表的别名

    再接着,查询出平均工资最低的部门

     在HAVING中使用子查询

    • 方法一:
    1. SELECT department_id
    2. FROM employees
    3. GROUP BY department_id
    4. HAVING AVG(salary)=(
    5. SELECT MIN(avg_salary)
    6. FROM(
    7. SELECT AVG(salary) avg_salary
    8. FROM employees
    9. GROUP BY department_id
    10. ) dep_avg_salary
    11. )
    •  方法二:用ALL关键字,就不用找出最低的部门平均工资。
    1. SELECT department_id
    2. FROM employees
    3. GROUP BY department_id
    4. HAVING AVG(salary)<=ALL(
    5. SELECT AVG(salary)
    6. FROM employees
    7. GROUP BY department_id
    8. )

     相关子查询

    #2.查询员工中工资大于所在部门平均工资的员工的last_name,salary和其department_id

    • 方法一:使用相关子查询,用WHERE关键字
    1. SELECT last_name,department_id,salary
    2. FROM employees e1
    3. WHERE salary>(
    4. SELECT AVG(salary)
    5. FROM employees e2
    6. WHERE e2.`department_id`=e1.`department_id`
    7. )
    •  方法二:在FROM中声明子查询

    from型的子查询,子查询是作为from的一部分,子查询要用()引起来,并且要给这个子查询取别名,把它当成一张“临时的虚拟的表”来使用

    (由于FROM后面有两个表,里面生成的那个新表也含有字段department_id,所以在查询的时候要声明一下departemnt_id是查询的哪个表里面的)

    1. SELECT last_name,e1.department_id,salary
    2. FROM employees e1,(
    3. SELECT AVG(salary) avg_sal,department_id
    4. FROM employees
    5. GROUP BY department_id)e2
    6. WHERE e1.`department_id`=e2.`department_id`
    7. AND e1.`salary`>e2.`avg_sal`

     #3.查询员工的id,salary,按照depertment_name排序(department_name在departments表中)

    在ODER BY后面跟子查询

    1. SELECT e.employee_id,e.salary,c.department_name
    2. FROM employees e,departments c
    3. WHERE e.`department_id`=c.`department_id`
    4. ORDER BY(
    5. SELECT department_name
    6. FROM departments d
    7. WHERE e.`department_id`=d.`department_id`
    8. )ASC

    #4.若employees 表中employee_id与job_history表中的employee_id相同的数目不小于2,输出这些相同id的员工的employee_id,last_name和其job_id

    1. SELECT employee_id,last_name,job_id
    2. FROM employees e
    3. WHERE 2<=(
    4. SELECT COUNT(*)
    5. FROM job_history
    6. WHERE employee_id=e.`employee_id`
    7. )

    #5.查询公司管理者的employee_id,和last_name信息 

    • 方法一:WHERE关键字过滤
    1. SELECT DISTINCT mgr.employee_id,mgr.last_name
    2. FROM employees emp,employees mgr
    3. WHERE emp.`manager_id`=mgr.`employee_id`
    • 方法二:自连接
    1. SELECT DISTINCT mgr.employee_id,mgr.last_name
    2. FROM employees emp JOIN employees mgr
    3. ON emp.`manager_id`=mgr.`employee_id`
    • 方法三:子查询
    1. SELECT employee_id,last_name
    2. FROM employees
    3. WHERE employee_id IN(
    4. SELECT DISTINCT manager_id
    5. FROM employees
    6. )
    • 方法四:EXISTS关键字
    1. SELECT mgr.employee_id,mgr.last_name
    2. FROM employees e1
    3. WHERE EXISTS(
    4. SELECT *#这块查询什么其实不重要,只要查询得到那么就返回TRUE
    5. FROM employees e2
    6. WHERE e1.`employee_id`=e2.`manager_id`
    7. )

    #6.查询departments表中,不存在于employees表中的部门的department_id和department_name

    • 方法一:外连接 
    1. SELECT d.department_id,d.department_name
    2. FROM departments d LEFT JOIN employees e
    3. ON d.`department_id`=e.`department_id`
    4. WHERE e.`department_id` IS NULL
    • 方法二:NOT EXISTS关键字
    1. SELECT department_id,department_name
    2. FROM departments d
    3. WHERE NOT EXISTS(
    4. SELECT *
    5. FROM employees e
    6. WHERE d.`department_id`=e.`department_id`
    7. #如果存在这样的记录那么就返回false,不要这条记录,如果不存在这样的记录那么就返回TRUE,这条记录就是要查询的记录
    8. );

  • 相关阅读:
    【AUTOSAR-Nm】-2.2-通过CAN/Lin...信号报告Nm状态机的跳转
    微擎模块 超人跑腿 1.7.1 后台模块+前端小程序,后台新增代办,代驾,家政模板自定义
    交叉熵函数和KL散度函数
    GiliSoft USB Lock v10.5.0 电脑USB设备管控软件
    debug技巧之远程调试
    #gStore-weekly | gStore最新版本0.9.1之BIND函数的使用
    在虚拟机中截图
    原生ajax
    Hadoop 启动!
    java数据结构与算法 --- 第十章 数结构基础
  • 原文地址:https://blog.csdn.net/wxxxx_xx/article/details/126264934