Skip to content

HAVING

一句话解释

HAVING 筛选分组创建通过 GROUP BY — 它是 如 WHERE, 但 applied 之后 aggregation 而不是之前.

用大白话说

HAVING 是如 WHERE, 但 for 分组. WHERE 筛选 individual 行之前它们是 分组; HAVING 筛选分组之后它们有 是创建通过 GROUP BY.

The key difference: you cannot use aggregate functions in WHERE (because rows have not been grouped yet), but you can in HAVING. For example, you cannot write WHERE COUNT(*) > 5, but you can write HAVING COUNT(*) > 5.

A common pattern is: filter rows with WHERE, group with GROUP BY, then filter groups with HAVING.

示例

sql
-- Users who placed more than 5 orders
SELECT user_id, COUNT(*) AS order_count
FROM orders
WHERE status = 'completed'
GROUP BY user_id
HAVING COUNT(*) > 5;

WHERE 筛选到 已完成顺序, GROUP BY 分组通过用户, HAVING keeps 仅分组使用更多 than 5.

相关术语

相关资源

相关课程

  • GROUP BY

    学习 GROUP BY and HAVING together.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表