How can I update value every 20 and 30 minutes? - mysql

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.

Related

Time-based operation in MySQL

I want to perform a time-based operation in MySQL. If the value in the table is 0 while it is 1, the system will start counting time automatically and the value that is 1 should automatically change to 0 at the end of 30 days.
You can use a view for this. Instead of storing the value, store the timestamp when the timing starts. Then just use logic to calculate how recent the value is:
create view v_mytable as
select t.*,
(value_timestamp < curdate() - interval 30 day) as value_flag
from mytable t;

Change column value if date is older than 1 hour in mysql/mariadb?

Say I have below SQL table:
+------------+---------------------+
| Status | Date |
------------+---------------------+
| Available | 2020-12-19 18:03:42 |
+------------+---------------------+
Basically I want MySQL to check the Date column every 3 minutes and if Date value is older than 1 hour, I want to change Status to Offline. Can we achieve such thing in SQL? Alternatively if there is easier solution in Django python, I am open to use that as well.
I would recommend doing this with a view:
create view v_t as
select (case when date + interval 1 hour < current_timestamp then 'Offline'
else status
end) as status,
. . . -- whatever other columns you have here
from t;
A view has the following advantages:
The value is automatically correct. Users of the view will see the 'Offline' status exactly after an hour.
There is no overhead on changing the data.
This works even if the entire system goes down and then comes back up.
You can actually set the status at your leisure -- say a job that runs once per day or per week at an off-time.
You can check and update the status in a single statement:
update mytable set status = 'Offline'
where date < current_timestamp - interval 1 hour;
Now we need to schedule the query to execute on a regular basis. For this, one would typically use MySQL's event scheduler:
create event event_update_status
on schedule every 3 minute
starts current_timestamp
ends current_timestamp + interval 1 day
do
update mytable set status = 'Offline'
where date < current_timestamp - interval 1 hour;

Updating data in MySQL in scheduled time

I want to UPDATE my data in scheduled time. My problem is that I cant equal the date that I enter in my database in the current real time date. For example, I have 2015/24/9 19:50:00 in my database, now I want to equal it to the current real time date so that I can update a specific row in the database. If I don't do that, the amount field will just multiply 5 in every row. I want to multiply the amount by 5 in a specific row and time
Code:
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = (the the current real time date);
DATETIME and TIMESTAMP values are, like floating-point values, difficult to compare for numerical equality. In other words, if it happens that NOW() = datetimestamp, it's a lucky accident. This is especially true when processing events: the event actually starts to run shortly after the scheduled time.
So, instead of saying something like this
`DATE` = NOW()
say something like this
`DATE` BETWEEN NOW() - INTERVAL 10 SECOND
AND NOW() + INTERVAL 10 SECOND
Of course, such a narrow time interval makes you critically dependent on the time accuracy of the event scheduler. You'd be better off adding a LAST_UPDATED column of DATETIME type to your table, then doing this update.
UPDATE messagesd
SET amount = amount * 5,
LAST_UPDATED = `DATE`
WHERE `DATE` >= NOW() - INTERVAL 1 MINUTE
AND (`DATE` > LAST_UPDATED OR LAST_UPDATED IS NULL)
That way, every time your event runs you'll update all the rows that are due for update, but haven't yet been updated. This is not dependent on the precise time an event runs. The - INTERVAL 1 MINUTE allows the event to be up to a minute late running and still function correctly.
If you need to schedule another update for the future for a particular row, change the value of the DATE column but don't touch the LAST_UPDATED column.
Are you just looking for CURDATE()?
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount * 5
WHERE DATE = CURDATE();
If you need the current real date time use mysql NOW()
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = NOW();

How to UPDATE data in MySQL after a scheduled time

I am new to SQL and I want to update the Amount in the amount field. For example in every month in 3 months I want to add 100 to THE 500 in the amount field in the database. And when the 3rd month comes It will automatically transfer the data to another table
This is the the code I have done so far, but its not working
CREATE EVENT myevent
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 2 MINUTE
DO
UPDATE message
SET amount*3
WHERE date = NOW();
Try to change the update command into:
UPDATE Message
SET amount = amount + 100
WHERE DATEDIFF(CURDATE(), date) >=value

How to decide in what hour event starts in phpmyadmin?

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