Mysql的取各分组的奇前N条数据

1.按指定排序获取数据:

select * from table order by type

 2.给获取的结果增加统计字段:

@num:=IF(@groups=`type`,@num+1,IF(@groups:=`type`,1,1)) as total 

 3.组合语句:

SELECT wid,name,type,@num:=IF(@groups=`type`,@num+1,IF(@groups:=`type`,1,1)) as total FROM w_websites ORDER BY type 

  



4.查询获取结果:
select * from ( ... ) where total<=N

猜你喜欢

转载自www.cnblogs.com/itwlp/p/11597112.html
今日推荐