Another way could be to get an ordered list using group_concat. It would be useless if you had a lot of data.
select group_concat(id order by insertdate desc separator ','), category from tablename group by category
or using subsamples (this is in mysql)
select category, (select id from test as test1 where test1.category = test.category order by insertdate desc limit 0,1) as recent1, (select id from test as test1 where test1.category = test.category order by insertdate desc limit 1,1) as recent2 from test group by category;
I know that the second option is not technically selected, since there are sub-queries, but this is the only way I can do this.
source share