简体   繁体   中英

Why isn't COUNT counting things in this MySQL statement?

This is a query on the Sakila sample database. I'm not sure why COUNT is returning '1' for every row - I want it to count the number of customers in that country/city/postal_code combination.

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

Any ideas on what I'm doing wrong?

Here is some of the output: 上面命令的输出

Because you are not getting multiple records per GROUP. The COUNT() function returns the number of records that match the same GROUP BY .

Also this query doesn't seem to work since you're selecting ID's but not grouping on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM