mysql> select
-> if (@cat = f_cat_id, @rn := @rn + 1, @rn := 1) rownum,
-> (@cat := f_cat_id) f_cat_id,
-> e.Id, f_hits, f_Status_id
-> from
-> (select * from tab_entries
-> order by id, f_cat_id, f_hits desc ) e,
-> (select (@cat := 0)) as x
-> ;
+--------+----------+------+--------+-------------+
| rownum | f_cat_id | Id | f_hits | f_Status_id |
+--------+----------+------+--------+-------------+
| 1 | 1 | 1 | 20 | 1 |
| 2 | 1 | 1 | 13 | 2 |
| 3 | 1 | 1 | 12 | 1 |
| 1 | 2 | 1 | 5 | 2 |
| 1 | 3 | 1 | 11 | 2 |
| 2 | 3 | 1 | 9 | 2 |
| 1 | 4 | 1 | 33 | 2 |
| 2 | 4 | 1 | 13 | 2 |
| 3 | 4 | 1 | 12 | 1 |
| 1 | 3 | 2 | 22 | 1 |
| 2 | 3 | 2 | 9 | 2 |
+--------+----------+------+--------+-------------+
11 rows in set (0.00 sec)
mysql>
mysql> select * from (
-> select
-> if (@cat = f_cat_id, @rn := @rn + 1, @rn := 1) rownum,
-> (@cat := f_cat_id) f_cat_id,
-> e.Id, f_hits, f_Status_id
-> from
-> (select * from tab_entries
-> order by id, f_cat_id, f_hits desc ) e,
-> (select (@cat := 0)) as x
-> ) o
-> where o.rownum<3;
+--------+----------+------+--------+-------------+
| rownum | f_cat_id | Id | f_hits | f_Status_id |
+--------+----------+------+--------+-------------+
| 1 | 1 | 1 | 20 | 1 |
| 2 | 1 | 1 | 13 | 2 |
| 1 | 2 | 1 | 5 | 2 |
| 1 | 3 | 1 | 11 | 2 |
| 2 | 3 | 1 | 9 | 2 |
| 1 | 4 | 1 | 33 | 2 |
| 2 | 4 | 1 | 13 | 2 |
| 1 | 3 | 2 | 22 | 1 |
| 2 | 3 | 2 | 9 | 2 |
+--------+----------+------+--------+-------------+
9 rows in set (0.00 sec)
mysql>