mysql 分类获取排行数据

建立表
CREATE TABLE `test` (
  `Id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL COMMENT '标题',
  `clicktime` int(11) default NULL COMMENT '点击次数',
  `mtype` int(11) default NULL COMMENT '类型',
  `goal` tinyint(3) default NULL COMMENT '分数',
  `create_date` date NOT NULL default '2012-01-01' COMMENT '日期',
  PRIMARY KEY  (`Id`),
  KEY `IND_TYPE` (`mtype`,`goal`,`clicktime`,`create_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


插入1万条数据后
查询每个分类的点击次数最高的10条数据。
select a.* 
from
(
select t1.*,(select count(Id)+1 from test 
where mtype=t1.mtype and clicktime>t1.clicktime ) 
as group_id
from test t1  where  t1.create_date='2012-04-29'
) a
where a.group_id<=10 


上面sql还是有些问题 如果有相同的点击次数,每个分类结果条数会超过10条,且性能不怎样  要12秒左右。
先记录,后期再优化

猜你喜欢

转载自xixian.iteye.com/blog/1499648