仅当记录包含超过20条相关记录时才包括记录

时间:2014-04-07 20:26:37

标签: sql

这是我使用的声明:

SELECT
    country.code as "Country Code",
    country.name as Country,    
    count(countrycode) as "Number Of Cities"
FROM
    city,
    country
WHERE
    city.countrycode = country.code
GROUP BY country.code, country.name;

我需要选择拥有超过20个城市的国家/地区。之后我将为此查询创建一个视图。

1 个答案:

答案 0 :(得分:3)

对聚合函数使用HAVING子句:

...
GROUP BY country.code, country.name
HAVING COUNT(countrycode) > 20;