最近写了个存储过程,配合事件使用,任务:每五分钟刷新表格的数据
过程:
BEGIN
DECLARE v_startTime BIGINT;
DECLARE v_step INT;
DECLARE i int DEFAULT 1;
DECLARE v_endTime BIGINT;
DECLARE v_date TIMESTAMP;
DECLARE v_time varchar(20);
set v_step = 60*5;#五分钟
set v_startTime = DATE_FORMAT(CURRENT_DATE() ,'%Y%m%d') * 1000000;
set v_endTime = v_startTime;
set autocommit=false;
delete from ts_user_onduty_history;
ALTER TABLE ts_user_onduty_history AUTO_INCREMENT=1;
delete from ts_user_onduty_xaxis;
# select DATEDIFF(DATE_FORMAT(CURRENT_DATE() ,'%Y%m%d') * 1000000,CURRENT_TIMESTAMP())
#五分钟
while i<24*(60/5) and v_endTime<DATE_FORMAT(CURRENT_TIMESTAMP(),'%Y%m%d%H%i%s') DO
set v_date = FROM_UNIXTIME(UNIX_TIMESTAMP(v_startTime) + i * v_step);
set v_endTime = DATE_FORMAT(v_date,'%Y%m%d%H%i%s') + 59;
set v_time = DATE_FORMAT(v_date,'%H:%i');
insert into ts_user_onduty_xaxis(x) values(v_time);
insert into ts_user_onduty_history(area_id,area_name,dept_id,dept_name , data_count, data_time)
select area_id,area_name,dept_id,dept_name ,count(*) data_count,v_time data_time
from (
select user_id,max(pass_datetime) max_pass_datetime from view2_pass_record_curdate where company_id=1 and pass_datetime BETWEEN v_startTime and v_endTime group by user_id
) t , view2_pass_record_curdate a
where a.user_id=t.user_id and a.pass_datetime = max_pass_datetime and device_direction=1 and company_id=1 and a.user_id>0
group by area_id,area_name,dept_id,dept_name;
set i = i+1;
#周文20180706
#IF i> 24*12 THEN set i = 1;delete from ts_user_onduty_history;delete from ts_user_onduty_xaxis; end if;
end WHILE;
commit;
set autocommit=true;
END
事件:
定义:
计划:
不执行的原因是这里,不支持5 -MINNUTE搭配。
正确的方式如下: