mysql 日期循环_如何在mysql存储过程中循环日期时间

即时通讯使用mysql存储过程,如何制作一个循环,每1小时变量strathour将有1小时的间隔然后返回查询的总数.当starthour小于11月的日期时,它将具有1小时的间隔并执行查询.

这是我的代码:

DELIMITER $$

CREATE DEFINER=`root`@`%` PROCEDURE `sp_asessiontime`(

out `total` int

)

BEGIN

declare `starthour`, `endhour` datetime;

set `starthour` = '2012-09-20 01:59:00';

set `endhour` = '2012-09-20 02:00:00';

select count(terminalcount.terminalids) into total from (

select distinct ts.TerminalID `terminalids` from

tmptransactiondetails td

inner join transactionsummary ts

on td.TransactionSummaryID = ts.TransactionsSummaryID

where

td.ServiceID = 4

and

td.TransactionType in ('D','W')

and

(ts.DateStarted >= starthour and ts.DateStarted < endhour)

or

(ts.DateEnded >= starthour and ts.DateEnded < endhour)

or

(ts.DateStarted < starthour and starthour <= ts.DateEnded)

)as terminalcount;

– 每1小时循环一次

while

starthour < '2012-11-01 01:59:00' do

select starthour + interval 1 hour;

select total as totalnumber;

end while;

END

十分感谢大家.


版权声明:本文为weixin_32073865原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。