having语句学习
转自:https://blog.csdn.net/u014401141/article/details/53010608
https://blog.csdn.net/qq_44761854/article/details/123185460
1.介绍
WHERE 搜索条件在进行分组操作之前应用;而 HAVING 搜索条件在进行分组操作之后应用。having后可以跟聚合函数,但where后面不行。
当同时含有where子句、group by 子句 、having子句及聚集函数时,执行顺序如下:
- 执行where子句查找符合条件的数据;
- 使用group by 子句对数据进行分组;对group by 子句形成的组运行聚集函数计算每一组的值;
- 最后用having 子句去掉不符合条件的组。
即,where 早于 group by 早于 having。
where后如果跟聚合函数形成的变量会报错:
Exception: Aggregate function count() is found in WHERE in query: While processing count() AS num. (ILLEGAL_AGGREGATION)