SQL中的HAVING和WHERE子句之间的区别

参见英文答案 > What’s the difference between HAVING and WHERE?                                    18个

SELECT column_name, aggregate_function(column_name) 
FROM table_name 
WHERE column_name operator value 
GROUP BY column_name 
HAVING aggregate_function(column_name) operator value

拥有和在哪里有什么区别

select … from上的过滤器

通过…对聚合结果进行筛选

所以,再看一下你的例子:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value

这里,WHERE column_name运算符值表示“返回table_name所在的结果
‘column_name运算符值’为true“.

只有在找到这些条件的所有结果后,才按column_name分组.

然后HAVING aggregate_function(column_name)运算符值表示“对于生成的聚合组,运行’aggregate_function(column_name)’并仅返回’aggregate_function(column_name)运算符值’为真的结果.”

相关文章
相关标签/搜索