因为mysql不支持数组,所以我这里采用的是插入临时表解决+循环(笨办法)。
我这里统计的是startime到endtime间每小时统计一次数据的存储过程。
DELIMITER $$;DROP PROCEDURE IF EXISTS GetM24HData_AllCount`$$CREATE  PROCEDURE `GetM24HData_AllCount`(in starttime varchar(100),in endtime varchar(100),in varM int)
BEGIN
declare ss int default 0 ;
declare ps int default 0 ;
declare pps int default 0 ;
declare i int default 0 ;
declare loopnum int default 0;
drop table if exists AllCount_all_temp1;
create table AllCount_all_temp1 (
starttime varchar(100),
setupsum int ,
personsum int ,
persum int 
);
if(endtime-starttime)=0 then
Set loopnum=24;
else
Set loopnum=(endtime-starttime)/3600;
end if;
while i<=loopnum do //统计循环开始
select count(distinct peerid) as cnt  into ss from peeridstat where peerid not in (select distinct peerid from peeridstat where time<=starttime-varM*24*3600) && time<=starttime && time>(starttime-varM*24*3600); 
select count(distinct peerid) as cnt into ps  from peeridstat where  time<=starttime && time>(starttime-varM*24*3600);
select count(peerid) as cnt  into pps from peeridstat where  time<=starttime && time>(starttime-varM*24*3600);
insert into AllCount_all_temp1 values(starttime,ss,ps,pps);
Set starttime=starttime+3600;
Set i=i+1;
end while;
END$$DELIMITER ;$$如果哪位有更好的思路可以讨论一下。