mysql資料庫超時自動改變訂單狀態 儲存過程 定時任務的運用
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;