How to update the status automatically after 10 days using event schedular? - mysql

I have sent_date and status in my table. I wanted to update the status automatically after 10 days.
Suppose sent_date is 24/03/2018 and status is Sent/Received. So on 04/04/2018, the status should update to Aborted automatically.
I know I have to use Date_Add but for testing purpose I am making use of Date_Sub.
What I tried:
CREATE EVENT rot ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE sent_date=DATE_SUB(CURDATE(), INTERVAL 1 day )
AND proposal_status = "Sent/Received"
So here,CURDATE takes the date of the system(24/03/2018),and it is updating the 23/03/2018 status to Aborted..This works fine.
CREATE EVENT rot ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE sent_date=DATE_SUB(sent_date, INTERVAL 1 day )
AND proposal_status = "Sent/Received"
sent_date in the table can be anything. So for this, it is not working properly. What changes are required so that the status can be automatically update after 10 days?

CREATE EVENT rot ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE sent_date=DATE(CURDATE()-10)
AND proposal_status = "Sent/Received"

Related

How to update the status automatically after 5 days?

I have sent_date and status column in my database table.currently status of the post is = Sent/Received. All I want to change the status = Aborted after 5days automatically.
What I tried:
CREATE EVENT reset ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE `sent_date`>=DATE_ADD( CURDATE(), INTERVAL 1 day )
AND proposal_status = "Sent/Received"---------Not worked
CREATE EVENT rot ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE DATE_ADD(sent_date, INTERVAL 1 day )>=NOW()
AND proposal_status = "Sent/Received"---------Not worked
CREATE EVENT rot ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE sent_date=CURDATE()
AND proposal_status = "Sent/Received"-----------Not Worked
How can I update the status automatically after 5 days? sent_date can be anything.
If sent_date is 26/03/2018 then on 01/04/2018, the status should update to Aborted automatically. How can I write the logic?
try CURDATE()-5
CREATE EVENT rot ON SCHEDULE EVERY 1 day DO
update barter_proposals
set proposal_status="Aborted"
WHERE sent_date=DATE(CURDATE()-5)
AND proposal_status = "Sent/Received"

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

Event not running properly

I need to create an automated event in MYSQL to update the status of orders to "Fulfilled" if current date is 2 days more than order date. My event needs to run once everyday.
Here is what I have created :
CREATE EVENT updorder
ON SCHEDULE EVERY 1 DAY
DO
update orders
set order_status = "Fulfilled"
where order_dt + INTERVAL 2 DAY < DATETIME
I also did this :
SET GLOBAL event_scheduler = ON;
SHOW PROCESSLIST
309 event_scheduler localhost NULL Daemon 125 Waiting for next activation NULL
What should I do to get this event to activate?
Your event is probably firing but not doing the intended UPDATE cause the below WHERE condition don't makes sense (DATETIME is a datatype)
where order_dt + INTERVAL 2 DAY < DATETIME
I think you meant to check less than current date time
where order_dt + INTERVAL 2 DAY < NOW()
(OR)
WHERE DATE_ADD(order_dt, INTERVAL 2 DAY) < NOW()

mysql scheduled events problems

I have a table reservation containing starttime,endtime,status. i wanted to create an event scheduler which checks whether the current time is greater than endtime . if it is greater then it sets the status to 0.
but i want that scheduler to run everyday from 6 am to 6pm at 5 minutes interval for forever. so far i have written a code
CREATE EVENT rescancel
ON SCHEDULE EVERY 5 MINUTE
STARTS 06:00:00
ENDS 18:00:00
DO
UPDATE reservation SET status = 0 WHERE CURTIME() > endtime
how can i make sure that this event runs everyday? so far from what i have read from different websites , this code runs for every 5 minutes from 6 am to 6pm only for that day when event is created. am i wrong? i am new to mysql triggers and events.
I use something similar to check days and times, so you could use:
DELIMITER ||
CREATE EVENT rescancel
ON SCHEDULE
EVERY 5 MINUTE
STARTS NOW()
ON COMPLETION PRESERVE
COMMENT ''
DO
BEGIN
IF CURRENT_TIME BETWEEN '06:00:00' AND '18:00:00' THEN
UPDATE reservation SET status = 0 WHERE CURTIME() > endtime;
END IF;
END ||
DELIMITER ;
Test the current time in the query:
CREATE EVENT rescancel
ON SCHEDULE EVERY 5 MINUTE
DO
UPDATE reservation
SET status = 0
WHERE HOUR(CURTIME()) BETWEEN 6 AND 17
AND CURTIME() > endtime

Create Trigger to delete rows that are >90 days old

Trying to create a trigger that will delete any record that is 90 days old. I used a trigger statement from stackoverflow that I have found and changed the statement a bit, but in my MySQL Workbench, I am getting a syntax error. I cannot seem to figure what is wrong.
Below is my query:
create trigger user_connections_dump after insert on user_connections
for each row
begin
delete from connection_time where Date('2014-06-09') > Date('now','-90 days')
end;
Your need looks more like an Event than a Trigger.
CREATE EVENT IF NOT EXISTS `Clean_Older_Than_90_days_logs`
ON SCHEDULE
EVERY 1 DAY_HOUR
COMMENT 'Clean up log connections at 1 AM.'
DO
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
References:
MySQL Event Scheduler on a specific time everyday
CREATE TRIGGER user_connections_dump
AFTER INSERT ON user_connections
FOR EACH ROW
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
You should be comparing the date column in the log table, not a literal date. Then you use DATE_SUB to subtract dates, not the Date function.
CREATE EVENT IF NOT EXISTS `Delete_Older_Than_90_Days`
ON SCHEDULE EVERY 1 DAY
STARTS STR_TO_DATE(DATE_FORMAT(NOW(),'%Y%m%d 0100'),'%Y%m%d %H%i') + INTERVAL 1 DAY
DO
DELETE LOW_PRIORITY FROM log WHERE log_date < DATE_SUB(NOW(),INTERVAL 90 DAY)