You can simulate the partitioned row_number using user variables and then limit rows and apply group_concat:
Consider the following table:
create table your_table (
id int primary key autoincrement,
category int,
value int
);
and data:
insert into your_table (category, value)
values
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
(2, 6), (2, 7), (2, 8), (2, 9), (2, 10),
(3, 11), (3, 12), (3, 13), (3, 14), (3, 15);
And we want is top 3 (in order of latest id) value per category concatenated:
select category,
group_concat(value order by id desc) as value_con
from (
select t.*,
@rn := if(@category = category, @rn + 1, if(@category := category,1, 1)) as seqnum
from your_table t
cross join (select @category := null, @rn := 0) x
order by t.category, t.id desc
) t
where seqnum <= 3
group by category;
Output:
category value_con
1 5,4,3
2 10,9,8
3 15,14,13
Here is a demo of this.