我在mysql表中有一个列,其数据类型为INT(11)。
如何搜索以获取此列中前10个最常出现的值?
答案 0 :(得分:27)
SELECT col, count(*)
FROM tablethingie
GROUP BY col
ORDER BY count(*) DESC
LIMIT 10
答案 1 :(得分:4)
尝试以下代码
SELECT colname, COUNT(*) AS cnt
FROM tablename
GROUP BY colname
ORDER BY cnt DESC
LIMIT 10
答案 2 :(得分:3)
TOP是MySQL不支持的关键字,但它在MSSQL中。
以下查询应该做你想要的(未经测试,但想法应该变得清晰):
SELECT column, COUNT(*) AS matches
FROM table
GROUP BY column
ORDER BY matches DESC
LIMIT 10
答案 3 :(得分:2)
尝试:
SELECT ColName, Count(1) AS occurances
FROM
table
GROUP BY
ColName
ORDER BY
occurances DESC
LIMIT
10