我需要在一个MySQL sheduler事件中删除+ update
查询:
DELETE FROM access WHERE d > 0;
UPDATE access SET d = 1 WHERE d = 0;
如何在一个MySQL事件中组合它们?
答案 0 :(得分:0)
- 首先设置
event_scheduler
ON
。
SET GLOBAL event_scheduler = ON;
- 现在创建活动(活动将安排在 5分钟 间隔)
DROP EVENT IF EXISTS `deleteUpdateEvent`;
delimiter $$
CREATE EVENT `deleteUpdateEvent` ON SCHEDULE EVERY 5 MINUTE STARTS '2016-05-17 00:00:00'
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
DELETE FROM access WHERE d > 0;
UPDATE access SET d = 1 WHERE d = 0;
END$$
delimiter;
示例:强>
假设您的access
表格中包含以下数据:
id d
1 66
2 71
3 -6
4 -7
5 0
当事件完成后执行( FIRST RUN ),您的访问表将如下所示:
id d
3 -6
4 -7
5 1
第二次运行后:
访问表:
id d
3 -6
4 -7
注意:强>
对于后续运行,除非您的新数据具有>= 0
值
答案 1 :(得分:0)
您可以尝试这样:
DELIMITER $$
CREATE EVENT `myEvent`
ON SCHEDULE EVERY 1 MINUTE STARTS 'yourStartTime'
ON COMPLETION NOT PRESERVE ENABLE
DO
BEGIN
DELETE FROM access WHERE d > 0;
UPDATE access
SET d = 1
WHERE d = 0;
END $$
DELIMITER ;