下面是定时器实现的代码(任务完全由mysql完成)
//查看event是否开启(mysql重启就默认关闭了--OFF代表关闭)
show variables like 'event_scheduler';
//如果没有开启的话执行下面的语句
set global event_scheduler='on'
//创建存储过程(使用游标来遍历数据)
CREATE PROCEDURE timer()
BEGIN
DECLARE temp_id int(11) default 1;
DECLARE cur CURSOR FOR SELECT p.id FROM t_product p,t_order o WHERE p.commodity_number =o.pro_num AND o.isCount =0;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET temp_id = null;
OPEN cur;
FETCH cur INTO temp_id;
WHILE(temp_id is not null) DO
UPDATE t_product SET order_count= order_count+1 WHERE id=temp_id;
UPDATE t_order set isCount=1 WHERE isCount=0;
FETCH cur into temp_id;
END WHILE;
CLOSE cur;
END;
//创建事件()
CREATE EVENT IF NOT EXISTS event_test
ON SCHEDULE EVERY 8 SECOND//每八秒执行一次 这个时间自己根据需求设置
ON COMPLETION PRESERVE
DO CALL timer();//调用存储过程