MYSQL 5.1 and later versions support the scheduled execution (event) function, which is similar to linux crontab.
First, check your MSYQL version and the SQL statement for querying version information:
Select VERSION ()
After confirming the version support, check whether the event is Enabled:
Show variables like '% sche % ';
If not, how can I enable it?
Show variables like '% sche % ';
Set global event_scheduler = 1;
Show variables like '% sche % ';
Generally, there are two types of scheduled execution:
1. Execute the task at intervals starting from a specific time point;
From now on, it is executed every 30 seconds.
Create event if not exists e_test
On schedule every 30 second starts now ()
On completion preserve
Do
INSERT into sdb_yoyi (yoyiscid, orderid) VALUES (2, 3)
2. Execute the task from a specific time point on the current day;
Execute the task at on the first day of each month.
Create event if not exists e_test2
On schedule every 1 month starts DATE_ADD (DATE_SUB (CURDATE (), interval day (CURDATE ()-1 DAY), INTERVAL 1 MONTH), INTERVAL 1 HOUR)
On completion preserve
Do
INSERT into sdb_yoyi (yoyiscid, orderid) VALUES (2, 3)
You can also close the event task:
Alter event e_test ON
Completion preserve disable;
Enable closed event tasks:
Alter event e_test ON
Completion preserve enable;