37,737
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) NOT NULL,
`b` int(11) NOT NULL,
`c` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
分析SQL
select a,b,c,rank from
(
select source.a,source.b,source.c,if(@cur_a=source.a,@rank:=@rank+1,@rank:=1) as rank,@cur_a:=source.a
from
(
SELECT a,b,c FROM mytable
group by a,b
order by a,c desc
) source,
(
select @rank:=0,@cur_a=null
) tmp
) result
having rank<=5