我正在使用MySQL,我有一个带有项目的数据库表和5个类别(也有一个ID字段,autoincr,PK)我必须选择每个类别的最新5项,当我使用group by它返回最新的一项每个类别,我如何在一个查询中获得每个类别的前5项?
谢谢
答案 0 :(得分:0)
你正好讨厌这个......
(select * from items where category_id=1 order by add_date desc limit 5)
union
(select * from items where category_id=2 order by add_date desc limit 5)
union
(select * from items where category_id=3 order by add_date desc limit 5)
union
(select * from items where category_id=4 order by add_date desc limit 5)
union
(select * from items where category_id=5 order by add_date desc limit 5);