I am trying to get this database query to return only the 3 most common states, but somehow I keep getting all the states returned, listed in descending order. Not sure where I am going wrong...?
SELECT state, COUNT(*) as count
FROM Contact
GROUP BY state
HAVING state NOT IN(' ')
ORDER BY COUNT(*) DESC
LIMIT 3
I don't think you're using HAVING
correctly.
I would simply use this query
SELECT state, COUNT(1) AS state_count
FROM Contact
WHERE state <> ' '
GROUP BY state
ORDER BY state_count DESC
LIMIT 3
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.