mysql分组后,取每组的前3条数据(并且有顺序)

就是首先分组,然后取出每组前3条数据,每组内的数据是排序的

不列出表结构及测试数据,只能这样大概写个思路了:

select a.*
from
(
select t1.*,(select count(*)+1 from 表 where 分组字段=t1.分组字段 and 排序字段<t1.排序字段) as group_id
from 表 t1
) a
where a.group_id<=3
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-08-06
select *
from tb k
where 3>(select count(*) from tb where k.分组字段=分组字段 and 你的排序字段>k.你的排序字段)