1:每月最后一天16:00
#show variables like 'event_scheduler'; #set GLOBAL event_scheduler = ON; #show procedure status; -- 设置好时区 set time_zone = '+8:00'; -- 开启事件调度器 set GLOBAL event_scheduler = 1; -- 选择数据库 # 如果原来存在该名字的任务计划则先删除 drop event if exists commission_inquiry_every_month; # 设置分隔符为 '$$' DELIMITER $$ # 创建计划任务 create event commission_inquiry_every_month on schedule every 1 month starts ''2018-05-31 18:00:00'' do # 开始该计划任务要做的事 begin -- 执行过程 CALL getInquiryEffective(); -- 结束计划任务 end $$ # 将语句分割符设置回 ';' DELIMITER ;
过程:
CREATE PROCEDURE getInquiryEffective () ###获取当月有效的评估数据,保持当月一份 BEGIN #1.删除:插入之前,现将执行时间当月的数据删除(18:00后计算为第二天,所以使用count_time为条件) DELETE FROM report_statistics_commission_inquiry WHERE type = 0 AND date_format(count_time, '%Y-%m') = date_format(now(), '%Y-%m'); #2.插入:获取最新的当前月份有效的评估数据 INSERT INTO `report_statistics_commission_inquiry` ( `user_id`, `order_code`, `execute_time`, `task_code`, `count_time`, `type`, `ext1`, `ext2`, `ext3` ) SELECT li.transactor_id, li.inquiry_code, li.execute_time, NULL, NOW(), 0, NULL, NULL, NULL FROM loan_inquiry li WHERE #上个月最后一天18:00以后的-->这个月最后一天18:00的数据 li.execute_time BETWEEN CONCAT( date_sub( last_day( date_sub(now(), INTERVAL 1 MONTH) ), INTERVAL 0 DAY ), ' 18:00:00' ) AND CONCAT( date_sub( last_day(now()), INTERVAL 1 DAY ), ' 18:00:00' ) AND li.`status` = 1 AND delete_flag = 0 AND li.admin_user_id IS NOT NULL GROUP BY li.inquiry_code; END;