MySQL - 根据大多数事件选择人名

时间:2011-04-30 08:54:24

标签: mysql

假设:

名为names的表格包含idfirstlast字段。

firstlast是单独的字段时,您如何找到排名前5位最受欢迎的人?这是我现在的非工作代码......

`SELECT *, COUNT(first) AS occurrences FROM `names` GROUP BY first ORDER BY occurrences DESC LIMIT 5`

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:9)

对于前5名,请尝试:

SELECT first, COUNT(*) AS occurrences 
FROM names
GROUP BY first 
ORDER BY occurrences DESC 
LIMIT 5

对于前5名,请尝试:

SELECT first, last, COUNT(*) AS occurrences 
FROM names
GROUP BY first, last
ORDER BY occurrences DESC 
LIMIT 5