Bhumika
Bhumika

Reputation: 13

Auto Trigger a Mysql query every day at specific time

I want to give my users bonus every day at 00:01 AM, so i am looking for an automatic trigger in mysql to update Bonus in users.

UPDATE users SET bonus = bonus + 10

How can i do this ?

Thank you

Upvotes: 0

Views: 4184

Answers (2)

GMB
GMB

Reputation: 222672

MySQL has a built-in scheduler that can be used to execute tasks according to a given schedule.

Here is how to create a scheduled event for your use case:

create event grant_user_bonus_daily
on schedule every 1 day
starts current_date + interval 1 day + interval 1 minute -- starts tomorrow at 00:01
do
    update users set bonus = bonus + 10;

Upvotes: 1

t1f
t1f

Reputation: 3181

SET GLOBAL event_scheduler = ON;

Example:

CREATE EVENT test_event_02
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
   INSERT INTO messages(message,created_at)
   VALUES('Test MySQL Event 2',NOW());

Full tutorial here

Official MySQL link

Upvotes: 0

Related Questions