简体   繁体   中英

mysql scheduled events problems

I have a table reservation containing starttime,endtime,status . i wanted to create an event scheduler which checks whether the current time is greater than endtime . if it is greater then it sets the status to 0.
but i want that scheduler to run everyday from 6 am to 6pm at 5 minutes interval for forever. so far i have written a code

CREATE EVENT rescancel  
ON SCHEDULE EVERY 5 MINUTE  
STARTS 06:00:00   
ENDS 18:00:00
DO  
UPDATE reservation SET status = 0 WHERE CURTIME() > endtime  

how can i make sure that this event runs everyday? so far from what i have read from different websites , this code runs for every 5 minutes from 6 am to 6pm only for that day when event is created. am i wrong? i am new to mysql triggers and events.

Test the current time in the query:

CREATE EVENT rescancel
ON SCHEDULE EVERY 5 MINUTE
DO
    UPDATE reservation
    SET status = 0
    WHERE HOUR(CURTIME()) BETWEEN 6 AND 17
    AND CURTIME() > endtime

I use something similar to check days and times, so you could use:

DELIMITER ||
CREATE EVENT rescancel
ON SCHEDULE
EVERY 5 MINUTE
STARTS NOW()
ON COMPLETION PRESERVE
COMMENT ''
  DO
  BEGIN
  IF CURRENT_TIME BETWEEN '06:00:00' AND '18:00:00' THEN
    UPDATE reservation SET status = 0 WHERE CURTIME() > endtime;
  END IF;
  END ||

DELIMITER ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM