如何将MySQL查询结果限制在前3位

时间:2014-07-02 03:34:58

标签: mysql

我试图让这个数据库查询只返回3个最常见的状态,但不知怎的,我继续得到所有返回的状态,按降序排列。不知道我哪里错了......?

SELECT state, COUNT(*) as count
FROM Contact
GROUP BY state
HAVING state NOT IN(' ')
ORDER BY COUNT(*) DESC
LIMIT 3

1 个答案:

答案 0 :(得分:1)

我认为您没有正确使用HAVING

我只想使用此查询

SELECT state, COUNT(1) AS state_count
FROM Contact
WHERE state <> ' '
GROUP BY state
ORDER BY state_count DESC
LIMIT 3

演示〜http://sqlfiddle.com/#!2/8fc57/1