MYSQL 存储过程返回多数据集合。

jobillcai 2007-11-05 11:55:56
因为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 ;$$

如果哪位有更好的思路可以讨论一下。
...全文
746 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jobillcai 2007-11-05
  • 打赏
  • 举报
回复
这是牺牲性能的做法了。
如果mysql能和oracle一样就好了。
懒得去死 2007-11-05
  • 打赏
  • 举报
回复
MYSQL只能这样干。
懒得去死 2007-11-05
  • 打赏
  • 举报
回复
如果是这样的话。建议在程序端操作。

57,064

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧