I have a table ‘comenzi’, each order has a value for in progress, and another for ready and data. Each order when it is created has the values for in progress and ready equal to 0. I want to update the values for in progress and ready with 1 min ,after 20 min, then 30 min after the order has been done, the value from data that has the form 2020-05-25 09:41:00.
I need a way to update these values, a function or an event in database.
I was dealing with something using events just yesterday on a pet project. You could use a similar approach.
This code uses a creation column, which is just a TIMESTAMP column with a default value of CURRENT_TIMESTAMP (because I don't have your data) and a value column (default value 0), which goes from 1 - 2 (you can modify this code to suit your needs).
The SET value uses CASEs to check how long ago the order was created. Then it updates the value accordingly. You can change up the event to update whichever columns / time cases you'll need.
CREATE DEFINER =`root`#`localhost` EVENT `progressEvent`
ON SCHEDULE EVERY 1 MINUTE STARTS '2020-09-01 00:00:00'
ON COMPLETION PRESERVE ENABLE DO UPDATE comenzi
SET `value` = (
CASE WHEN (creation < (CURRENT_TIMESTAMP - INTERVAL 30 MINUTE))
THEN 2
CASE WHEN (creation < (CURRENT_TIMESTAMP - INTERVAL 20 MINUTE))
THEN 1
END)
WHERE value < 2
Edit: Changed it up so that it checks every minute and only 20/ 30 minutes.
I have an event that deletes record on a table that are a month old. But it does not seem to execute.
DELIMITER $
CREATE EVENT delete_wykofile_back
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 YEAR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM factory.wyko_file_backup WHERE date_inserted < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END$
DELIMITER ;
What I've done so far:
I have followed and set global event_scheduler to ON based from this question
I've also check this question, but to no avail.
There's also no answer from this question.
The account im using is a superuser based from this.
SHOW EVENTS;
SHOW PROCESSLIST;
I am using MySQL Workbench 6.2. I am running the event with 1 minute interval for testing.
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
needs to be the current date or a past date. Right now its set up to start on 12/4/2015.
I have an MySQL event that deletes any rows older than 1 month. I now want to change that so it deletes rows older than 6 months (it cleans up a database for messages sent in a game).
My (edited) MySQL event is:
DELETE FROM messages WHERE date < (NOW() - INTERVAL 6 MONTH)
The error I get is:
The following query has failed: "CREATE DEFINER=`admin`#`localhost` EVENT `delete msgs` ON SCHEDULE EVERY 0 DAY_HOUR STARTS '2013-07-19 18:00:00' ON COMPLETION PRESERVE ENABLE DO DELETE FROM messages WHERE date < (NOW() - INTERVAL 6 MONTH)"
MySQL said: #1542 - INTERVAL is either not positive or too big
The event functions fine but I can't even save it as it is now. If I open the event and immediately save I get the same error.
Juergen d basically answered my question in a comment. The interval was set to "EVERY 0 DAY_HOUR" while in phpmyadmin it was set to "EVERY '0_10' DAY_HOUR". I was able to save it before with the '0_10' but apparently that doesn't work anymore, or maybe never worked (even though the event did it's thing).
I changed that to "EVERY 10 HOUR" and now it saves fine.
if i'm trying to create an event in this page :
Now i have set it to run every 1 month, but how do i set at what time?
i want the report to save all record from last month into a table using
l.created_on <= DATE_SUB(NOW(), INTERVAL 1 month)
So it will run at 1/1/12 00:00:00 i guess but i'd rather wait 2 minutes just in case something was inserted in the exact same moment, so i can i set that while using phpMyAdmin?
What I meant was set the time here... That's a common trigger for date as well as time
Here's my query
CREATE EVENT
RESET ON SCHEDULE AT TIMESTAMP DO
UPDATE `ndic`.`students`
SET `status` = '0';
How can I update status to "0" at 1 pm every day.
What can I use instead of TIMESTAMP?
This might be too late for your work, but here is how I did it. I want something run everyday at 1AM - I believe this is similar to what you are doing. Here is how I did it:
CREATE EVENT event_name
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
# Your awesome query
The documentation on CREATE EVENT is quite good, but it takes a while to get it right.
You have two problems, first, making the event recur, second, making it run at 13:00 daily.
This example creates a recurring event.
CREATE EVENT e_hourly
ON SCHEDULE
EVERY 1 HOUR
COMMENT 'Clears out sessions table each hour.'
DO
DELETE FROM site_activity.sessions;
When in the command-line MySQL client, you can:
SHOW EVENTS;
This lists each event with its metadata, like if it should run once only, or be recurring.
The second problem: pointing the recurring event to a specific schedule item.
By trying out different kinds of expression, we can come up with something like:
CREATE EVENT IF NOT EXISTS `session_cleaner_event`
ON SCHEDULE
EVERY '1 13' DAY_HOUR
COMMENT 'Clean up sessions at 13:00 daily!'
DO
DELETE FROM site_activity.sessions;
Update, long over-due: The interval with the unit "DAY_HOUR" takes an expression that looks like 'day-number hour-number'. I have updated the oft-accepted solution to match that. Thanks, commenters for clarifying!
Try this
CREATE EVENT event1
ON SCHEDULE EVERY '1' DAY
STARTS '2012-04-17 13:00:00' -- should be in the future
DO
-- your statements
END
DROP EVENT IF EXISTS xxxEVENTxxx;
CREATE EVENT xxxEVENTxxx
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 1 HOUR)
DO
--process;
¡IMPORTANT!->
SET GLOBAL event_scheduler = ON;
My use case is similar, except that I want a log cleanup event to run at 2am every night. As I said in the comment above, the DAY_HOUR doesn't work for me. In my case I don't really mind potentially missing the first day (and, given it is to run at 2am then 2am tomorrow is almost always the next 2am) so I use:
CREATE EVENT applog_clean_event
ON SCHEDULE
EVERY 1 DAY
STARTS str_to_date( date_format(now(), '%Y%m%d 0200'), '%Y%m%d %H%i' ) + INTERVAL 1 DAY
COMMENT 'Test'
DO
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL recurring Event',NOW());