MySQL 取分类后的前n条

select *
from (SELECT @num:=0,@AAA:= 0) temp join
(
   select 
   @num := if(@AAA = AAA, @num + 1, 1) as row_number,
   @AAA := AAA as Dummy,
   mt.* 
  from table1 mt 
  where Created_Time BETWEEN DATE_SUB(CURRENT_TIMESTAMP,INTERVAL 1 DAY) and CURRENT_TIMESTAMP 
  order by AAA, ID desc
) as x where x.row_number <= 3;
posted on 2013-03-12 17:06  思静  阅读(618)  评论(0编辑  收藏  举报