mysql数据库超时自动改变订单状态 存储过程 定时任务的运用

时间:2022-08-15 23:29:03

DELIMITER |

DROP PROCEDURE IF EXISTS e_test |
CREATE PROCEDURE e_test()
BEGIN
update hb_hongbao set addtime=SYSDATE(15) where SYSDATE(15)>over_time and status=1;

IF EXISTS(SELECT id FROM hb_hongbao WHERE SYSDATE(15) < over_time and status=1) THEN
UPDATE hb_hongbao2 SET addtime = SYSDATE(15) WHERE uid NOT IN (SELECT id FROM hb_hongbao WHERE SYSDATE(15) < over_time and status=1);
END IF;
END

|
DELIMITER ;
#超时改变hb_hongbao时间 改变hb_hongbao2时间
select `name` from mysql.proc where db = 'hongbao' and `type` = 'PROCEDURE';

#删除存储过程
DROP PROCEDURE e_test;

2.创建定时器取名为event_test

SET GLOBAL event_scheduler = 1;
#开启event_scheduler sql指令是否开启
SHOW VARIABLES LIKE '%event_scheduler%';
CREATE EVENT IF NOT EXISTS event_test

ON SCHEDULE EVERY 1 SECOND STARTS '2018-12-19 11:00:00'

ON COMPLETION PRESERVE

DO CALL e_test();

1 SECOND,3 minute,5 hour,9 day,1 month,1 quarter(季度),1 year
创建一个定时器,2018-12-19 11:00:00开始执行,每间隔1秒触发一次事件,即每个一秒执行一次上面定义的e_test这个存储过程。

select name from mysql.event;
查看所有定时器名字

DROP EVENT [IF EXISTS] event_test;
删除定时任务

3.这个是最简单但是也是最重要的,我们要手动的启动这个定时器,要不然是没法工作的。

ALTER EVENT event_test ON

COMPLETION PRESERVE ENABLE;