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
Release: 2024-11-29 11:54:10
Original
290 people have browsed it

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

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)
Copy after login

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template