1. Use a cursor to create a stored procedure SQL: createPROCEDUREdaily_max_avg_online () BEGINdeclaremax_onlineint; declareavg_onlineint; declaretimeTIMESTAMP; declar 1. Use a cursor to create a stored procedure
SQL creation:
create PROCEDURE daily_max_avg_online()BEGINdeclare max_online int;declare avg_online int;declare time TIMESTAMP;declare cur cursor for select MAX(online_num),round(avg(online_num)),DATE_FORMAT(statistical_time ,'%y-%m-%d') from real_time_online_statistics group by DATE_FORMAT(statistical_time ,'%y-%m-%d');open cur;cur:loopFETCH cur into max_online,avg_online,time;insert into daily_max_avg_online_statistics(id,max_online_num,avg_online_num,statistical_time) values(UUID(),max_online,avg_online,time); end loop cur;close cur;END;
Navicat creation:
1. click functions in the database --> Create function
2. click finish all the way to go to the storage process creation interface.
3. Save (Ctrl + S) --> enter the name of the stored procedure --> complete the creation of the stored procedure
2. create a timer to call the stored procedure
1. click event --> Create Event --> enter the interface --> enter the stored procedure that needs to be called regularly
2. set the scheduled task. here, the task is set to run once a day and the start time is 15:08:00.
3. the constant GLOBAL event_scheduler of MySQL must be on or 1 -- check whether the timer show variables like "% event_scheduler % 'is enabled; -- enable the timer 0: off 1: on set global event_scheduler = 1; or set global event_scheduler = ON; Over