我的数据库duplicate_id
中有一个表,其中包含多个ID,并且还包含许多重复的ID。我一直试图做的是对表格duplicate_id
中重复次数最多的五个ID进行排序。请让我知道我该怎么做
表格结构: ID |消息
预期输出:
ID |重复次数
201 8
212 7
205 5
209 3
229 2
答案 0 :(得分:2)
SELECT ID, COUNT(*) AS `Number of repeats`
FROM duplicate_id
GROUP BY ID
ORDER BY COUNT(*) DESC
LIMIT 5
答案 1 :(得分:1)
按顺序尝试排序结果:
Select * from table order by repeats desc limit 5
答案 2 :(得分:0)
嗯...试试这个(我不知道mysql,所以我不得不猜)
select ID,
count(*) as 'Number of Repeats'
from duplicate_ID
group by ID
order by 2
另一种方法是
select ID, 'Number of Repeats'
from (
select ID,
count(*) as 'Number of Repeats'
from duplicate_ID
group by ID
) x
order by 'Number of Repeats'