假设:
名为names
的表格包含id
,first
和last
字段。
当first
和last
是单独的字段时,您如何找到排名前5位最受欢迎的人?这是我现在的非工作代码......
`SELECT *, COUNT(first) AS occurrences FROM `names` GROUP BY first ORDER BY occurrences DESC LIMIT 5`
任何帮助将不胜感激!
答案 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