48

Here's my query

CREATE EVENT
RESET ON SCHEDULE AT TIMESTAMP DO
UPDATE `ndic`.`students`
SET `status` = '0';  

How can I update status to "0" at 1 pm every day.

What can I use instead of TIMESTAMP?

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
gin
  • 873
  • 2
  • 12
  • 23

6 Answers6

66

This might be too late for your work, but here is how I did it. I want something run everyday at 1AM - I believe this is similar to what you are doing. Here is how I did it:

CREATE EVENT event_name
  ON SCHEDULE
    EVERY 1 DAY
    STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
  DO
    # Your awesome query
Andreas
  • 970
  • 18
  • 28
  • 5
    Is `TIMESTAMP(CURRENT_DATE)` necessary? Seems to work with just `CURRENT_DATE + Interval ..` – Coloured Panda Mar 19 '17 at 20:04
  • 2
    This is incredible! I had not realized how the "starts" was calculated, until I executed the consultation and then I said: it means "Tomorrow at 00:01:00" ... Thank you very much! – Daniel Azamar Jul 05 '19 at 04:07
42

The documentation on CREATE EVENT is quite good, but it takes a while to get it right.

You have two problems, first, making the event recur, second, making it run at 13:00 daily.

This example creates a recurring event.

CREATE EVENT e_hourly
    ON SCHEDULE
      EVERY 1 HOUR
    COMMENT 'Clears out sessions table each hour.'
    DO
      DELETE FROM site_activity.sessions;

When in the command-line MySQL client, you can:

SHOW EVENTS;

This lists each event with its metadata, like if it should run once only, or be recurring.

The second problem: pointing the recurring event to a specific schedule item.

By trying out different kinds of expression, we can come up with something like:

CREATE EVENT IF NOT EXISTS `session_cleaner_event`
ON SCHEDULE
  EVERY '1 13' DAY_HOUR
  COMMENT 'Clean up sessions at 13:00 daily!'
  DO
    DELETE FROM site_activity.sessions;

Update, long over-due: The interval with the unit "DAY_HOUR" takes an expression that looks like 'day-number hour-number'. I have updated the oft-accepted solution to match that. Thanks, commenters for clarifying!

olleolleolle
  • 1,918
  • 16
  • 20
  • 7
    This doesn't work for me. I tried with 10 DAY_HOUR and it simply executed immediately and scheduled itself. Nor do I see why it would. As I read the MySQL docs, DAY_HOUR is a way of specifying days and hours from now - it is still an interval. Or have I completely misunderstood? – Adam Jan 31 '13 at 08:05
  • 1
    Agree with @Adam. This doesn't seem to meet the requirement of running daily at 1pm. based on a few quick tests I think it would end up executing every 13 hours. `select current_timestamp() ,date_add(current_timestamp(), interval 13 day_hour);` – Phil Feb 13 '18 at 21:57
  • `CREATE EVENT e_specific_time ON SCHEDULE SELECT end_date FROM table_name WHERE end_date=CURRENT_DATE DO UPDATE active=0 FROM table_name WHERE end_date=CURRENT_DATE;` How can I run this? – Ankit Jindal Sep 04 '20 at 05:11
21

Try this

CREATE EVENT event1
ON SCHEDULE EVERY '1' DAY
STARTS '2012-04-17 13:00:00' -- should be in the future
DO
-- your statements
END
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
16
DROP EVENT IF EXISTS xxxEVENTxxx;
CREATE EVENT xxxEVENTxxx
  ON SCHEDULE
    EVERY 1 DAY
    STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
  DO
    --process;

¡IMPORTANT!->

SET GLOBAL event_scheduler = ON;
mercu
  • 160
  • 1
  • 6
  • 1
    SET GLOBAL event_scheduler = ON; -> used to activate mysql events, if not created but not run ever. http://dev.mysql.com/doc/refman/5.1/en/events-configuration.html – mercu May 16 '14 at 15:09
15

My use case is similar, except that I want a log cleanup event to run at 2am every night. As I said in the comment above, the DAY_HOUR doesn't work for me. In my case I don't really mind potentially missing the first day (and, given it is to run at 2am then 2am tomorrow is almost always the next 2am) so I use:

CREATE EVENT applog_clean_event
ON SCHEDULE 
    EVERY 1 DAY
    STARTS str_to_date( date_format(now(), '%Y%m%d 0200'), '%Y%m%d %H%i' ) + INTERVAL 1 DAY
COMMENT 'Test'
DO 
Adam
  • 6,539
  • 3
  • 39
  • 65
  • Couldn't you use `CURRENT_DATE()` instead of `str_to_date( date_format(now(), '%Y%m%d 0200'), '%Y%m%d %H%i' )`? Anyway thanks. – Linus Apr 11 '14 at 14:00
  • Yes - except that I want to be able to specify that it runs at 0200 each day. If you use current_date then it would run each day at whatever time you happened to run the script to create it. – Adam Apr 11 '14 at 16:58
  • 1
    Oh, I thought CURRENT_DATE returned a date with format: yyyy-MM-dd meaning that the time would be 00:00 but perhaps I wasn't right. – Linus Apr 11 '14 at 17:09
  • 2
    My appologies. You were right. I was getting my SQL implementations mixed up - I also work with Oracle pl/sql and there current_date is a date object containing the current date and time in the users timezone - more like MySQLs now(). So yes, if you want it run at 0000 then curent_date would probably work. If you want to specify the time ir runs each day, however, then you need my solution above. – Adam Apr 11 '14 at 17:31
  • 2
    ah, good to know. Not going to be picky but you could actually do something like this `(CURRENT_DATE() + INTERVAL X HOUR) + INTERVAL 1 DAY` but as you said, with Oracle it might not work the same. Thank you for the info. – Linus Apr 11 '14 at 20:25
2
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
   INSERT INTO messages(message,created_at)
   VALUES('Test MySQL recurring Event',NOW());
Artjom B.
  • 61,146
  • 24
  • 125
  • 222