⌘+k ctrl+k
1.1.3 (stable)
Search Shortcut cmd + k | ctrl + k
HAVING Clause

HAVING 子句可以在 GROUP BY 子句之后使用,以在分组完成后提供过滤条件。在语法上,HAVING 子句与 WHERE 子句相同,但 WHERE 子句在分组之前发生,而 HAVING 子句在分组之后发生。

Examples

计算addresses表中属于每个不同city的条目数量,过滤掉计数低于50的城市:

SELECT city, count(*)
FROM addresses
GROUP BY city
HAVING count(*) >= 50;

计算每个城市每条street_name的平均收入,过滤掉平均income大于中位数income两倍的城市:

SELECT city, street_name, avg(income)
FROM addresses
GROUP BY city, street_name
HAVING avg(income) > 2 * median(income);

Syntax