mysql 分组后筛选

分组后筛选的条件,是在一级筛选的结果上再做筛选,就放在最后,如果筛选和一级筛选的结果无关,则放在中间

#案例1:查询哪个部门的员工个数>2

SELECT 
    COUNT(*),department_id
FROM
    employees
GROUP BY
    department_id
HAVING
    COUNT(*)>2;

#:查询每个工种有奖金的员工的最高工资>12000的工种编号和最高工资

SELECT
    MAX(salary),job_id
FROM
    employees
WHERE
    commission_pct IS NOT NULL 
GROUP BY
    job_id
HAVING
    MAX(salary)>12000; #筛选12000放在分组后是因为,要等Max计算完再开始筛选

#查询领导编号>102的每个领导手下的员工的最低工资>5000的领导编号,以及其最底工资

SELECT
    MIN(salary),manager_id
FROM
    employees
WHERE
    manager_id>102
GROUP BY
    manager_id
HAVING MIN(salary)>5000;
    原文作者:wx5d21d5e6e5ab1
    原文地址: https://blog.51cto.com/14437184/2436639
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞