sql 查询每个分组下的前几条记录并排序

2024-12-02 21:38:14
推荐回答(1个)
回答1:

比如 表 table1,分类 type,读取分类前5条,合并在联查,并且排序
select * from
(
SELECT top 5 * FROM table1 where type=1
UNION
SELECT top 5 * FROM table1 where type=2
UNION
SELECT top 5 * FROM table1 where type=3
) as table_new order by id desc