我试图让这个数据库查询只返回3个最常见的状态,但不知怎的,我继续得到所有返回的状态,按降序排列。不知道我哪里错了......?
SELECT state, COUNT(*) as count
FROM Contact
GROUP BY state
HAVING state NOT IN(' ')
ORDER BY COUNT(*) DESC
LIMIT 3
答案 0 :(得分:1)
我认为您没有正确使用HAVING
。
我只想使用此查询
SELECT state, COUNT(1) AS state_count
FROM Contact
WHERE state <> ' '
GROUP BY state
ORDER BY state_count DESC
LIMIT 3