选择MYSQL中每个组的前5条记录

时间:2018-12-07 21:21:54

标签: mysql group-by aggregate-functions

我正在尝试获取一个分组的结果,每个组仅排前5行。.混淆操作方法。

以下是查询:

SELECT O.rest_brId as BRID, O.`reason` as REASON , count(O.reason) as 
OCCURENCES 
FROM orders O 
WHERE O.status = 4 
GROUP BY BRID, REASON 
HAVING count(O.reason)

当前结果是: result

我想要的是每个BRID都具有<= 5行,且TOP 5 MAX号。发生次数

怎么办?谷歌搜索,但感到困惑。

1 个答案:

答案 0 :(得分:1)

我无法发表评论,但是您是否不需要先ORDER BY OCCURENCES DESC然后再LIMIT 5