Select top 5 records of each group in MYSQL : <p>I am trying to fetch a grouped result, with only top 5 rows for each group.. Confused how to do it..</p>

<p>Here's the query:</p>

<pre><code>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)
</code></pre>

<p>There current result is :
<a href="https://i.stack.imgur.com/LVvZT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LVvZT.png" alt="result"></a></p>

<p>What I want is that every BRID to have &lt;= 5 rows, with TOP 5 MAX no. of OCCURENCES</p>

<p>How to do? Googled, but got confused.</p>
