id |  unqid
    ___|________
    1  |  v1
    2  |  v1
    3  |  v1
    4  |  v1
    5  |  v4
    6  |  v4
    7  |  v9
    8  |  v9
    9  |  v9
   10  |  v2
   11  |  v7


我需要获取最重复的值,限制为5,例如
v1
v9
v4
v2
v7

我试过了这段代码,但什么也没显示

SELECT unqid, COUNT(*) TotalCount FROM votes GROUP BY unqid HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC

最佳答案

我不确定这是否仅适用于Oracle,因此如果不适用于您,则表示歉意,但我会这样做

select unqid, TotalCount
  from (
    select unqid, count(*) as TotalCount
    from votes
    group by unqid
    order by TotalCount desc
  )
where TotalCount > 1

关于mysql - 如何在数据库中找到前5个重复值并获得5个结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23399797/

10-11 08:02