CREATE EVENT `set_trips_finished`
ON SCHEDULE EVERY 1 DAY STARTS '2015-08-25 01:50:00'
ON COMPLETION PRESERVE
DO BEGIN
UPDATE trips
SET status = 0
WHERE date(created_at) < curdate();
END;
是计划任务。但这些领域并没有得到更新。当我只运行查询时 - 字段得到更新。
我有另一个计划任务,语法相同,只计划在5分钟后运行,并且运行正常。
我不明白为什么这个任务不会运行,或者查询是否没有更新表格......有什么建议吗?
更新
我删除了另一个计划任务(正在运行的任务),并再次设置它们,现在都不会触发......
答案 0 :(得分:1)
查看事件调度程序是否正在运行:
show variables where variable_name='event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
<强>都能跟得上强>
创建我的测试表:
create table trips
( id int auto_increment primary key,
status int not null,
created_at date not null
);
insert trips(status,created_at) values (0,'2014-09-09');
创建我的活动:
DELIMITER $$
CREATE EVENT `set_trips_finished`
ON SCHEDULE EVERY 1 MINUTE STARTS '2015-08-23 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
UPDATE trips
SET status = status+1
WHERE date(created_at) < curdate();
END;$$
DELIMITER ;
按架构名称列出所有事件:
show events from so_gibberish;
or
show events\G; -- <--------- I like this one from mysql> prompt
show events; -- <--------- from workbench / sqlyog
*************************** 1. row ***************************
Db: so_gibberish
Name: set_trips_finished
Definer: GuySmiley@localhost
Time zone: SYSTEM
Type: RECURRING
Execute at: NULL
Interval value: 1
Interval field: MINUTE
Starts: 2015-08-23 00:00:00
Ends: NULL
Status: ENABLED
Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
查看可能更新状态的数据:
select * from trips;
+----+--------+------------+
| id | status | created_at |
+----+--------+------------+
| 1 | 0 | 2014-09-09 |
+----+--------+------------+
好吧,我可以整天等待,事件甚至没有打开
SET GLOBAL event_scheduler = ON; -- turn her on
show variables where variable_name='event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
等几分钟(注意我的活动每分钟运行一次)
select * from trips;
+----+--------+------------+
| id | status | created_at |
+----+--------+------------+
| 1 | 3 | 2014-09-09 |
+----+--------+------------+
事件已经运行了3次。好的,看起来不错。
SET GLOBAL event_scheduler = OFF; -- turn her off if so desired