HAVING
语法可以对分组之后的数据再做SELECT筛选
用HAVING进行筛选
SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias, …
FROM mytable
WHERE condition
GROUP BY column
HAVING group_condition;
HAVING
和 WHERE
语法一样,只不过作用的结果集不一样. 在我们例子数据表数据量小的情况下可能感觉 HAVING
没有什么用,但当你的数据量成千上万属性又很多时能帮上大忙 .
例:
语句:(统计Artist角色的雇员数量)
SELECT count(Role) FROM employees
where Role = "Artist"
语句:(按角色统计一下每个角色的雇员数量)
SELECT count(*),Role FROM employees
GROUP BY Role
语句:(算出Engineer角色的就职年份总计)
SELECT SUM(Years_employed) FROM employees
GROUP BY ROLE
HAVING Role = "Engineer"