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

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

DDD
DDDOriginal
2024-11-29 11:54:10201browse

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

Scheduling MySQL Events at a Specific Time Each Day

In MySQL, the Event Scheduler allows you to automate tasks based on predefined schedules. However, the syntax you provided (CREATE EVENT...DO UPDATE...AT TIMESTAMP) is outdated.

Solution:

To create an event that updates a status field to "0" at 1 pm every day, follow these steps:

  1. Use EVERY Day Trigger: Replace AT TIMESTAMP with EVERY 1 DAY in the CREATE EVENT syntax. This triggers the event to run once every day.
  2. Schedule Event Time: Append STARTS (TIMESTAMP(CURRENT_DATE) INTERVAL 1 DAY INTERVAL 1 HOUR) to the syntax. This schedules the event to start at 1 pm (offset by 1 day and 1 hour from the current time).

Revised Query:

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';

Alternative to TIMESTAMP:

The STARTS statement uses TIMESTAMP(CURRENT_DATE) as the starting point for the schedule. However, you can also use the NOW() function, which returns the current time.

STARTS (NOW() + INTERVAL 1 DAY + INTERVAL 1 HOUR)

The above is the detailed content of How to Schedule a Daily MySQL Event to Run 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