这是对Sakila示例数据库的查询。我不确定为什么COUNT会回来' 1'对于每一行 - 我希望它计算该国家/城市/邮政编码组合中的客户数量。
SELECT country.country_id, country.country, city.city_id, city.city, address.postal_code, COUNT(*) AS 'Customer Count'
FROM address
INNER JOIN city ON city.city_id = address.city_id
INNER JOIN country ON country.country_id = city.country_id
INNER JOIN customer ON customer.address_id = address.address_id
GROUP BY country.country, city.city, address.postal_code
关于我做错的任何想法?
以下是一些输出:
答案 0 :(得分:3)
因为您没有获得每个GROUP的多个记录。
COUNT()
函数返回与相同GROUP BY
匹配的记录数。
此查询似乎无法正常工作,因为您选择了ID但未对其进行分组。