Home >Database >Mysql Tutorial >How Can I Schedule a MySQL Event to Run Daily at a Specific Time?

How Can I Schedule a MySQL Event to Run Daily at a Specific Time?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-27 00:26:16585browse

How Can I Schedule a MySQL Event to Run Daily at a Specific Time?

Executing MySQL Event Scheduler at a Specific Time Daily

Imagine you have a database and need to update the status of records to "0" every day at 1 pm. Here's a query that might initially come to mind:

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

But that query uses TIMESTAMP, which doesn't allow for scheduling at a specific time. So, how can we achieve this result?

The answer lies in the STARTS keyword. Here's a modified query that executes the event every day at 1 pm:

CREATE EVENT event_name
  ON SCHEDULE
    EVERY 1 DAY
    STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
  DO
    UPDATE `ndic`.`students`
    SET `status` = '0';

This query starts the event at (CURRENT_DATE 1 DAY 1 HOUR) and repeats it every 24 hours thereafter. This ensures that the event triggers at 1 pm every day, as desired.

The above is the detailed content of How Can I Schedule a MySQL Event to Run Daily at a Specific Time?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn