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
Release: 2024-12-27 00:26:16
Original
653 people have browsed it

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';  
Copy after login

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';
Copy after login

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 of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template