Schedule MySQL Queries - php

How can I schedule a query to run using the MySQL scheduler (if that's the best method)? I followed the instructions from the link here but I'm a bit lost.
I would like to run the following query every 30 minutes on a particular database that we have.
update REQUESTS set status='expired' where status='pending' and date_sub(now(), interval 15 minute) > req_time;

delimiter //
CREATE EVENT IF NOT EXISTS your_event
ON SCHEDULE EVERY 30 MINUTE
STARTS '2014-07-25 12:00:00'
ON COMPLETION PRESERVE ENABLE
DO
update REQUESTS
set status='expired'
where status='pending'
and date_sub(now(), interval 15 minute) > req_time;
//

Related

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
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;
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

PHP - How do i delete data from database automatically after 24 hour of insertion

I want to delete data from database after every 24hours insertion.
For example: A user is added today and he is not active, after 24hours the user will be automatically deleted.
I intend doing this without cron because cron is not in my local/offline server.
Please i really need your help.
You could simply use
DELETE FROM your_table
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))
and use the mysql scheduled events
could be somethings like this
CREATE EVENT my_dayly_event
ON SCHEDULE
EVERY 1 DAY
COMMENT 'delete old values.'
DO
DELETE FROM your_table
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))
anyway take a look a this doc. https://dev.mysql.com/doc/refman/5.7/en/create-event.html ... could be useful

Mysql using only a time in date_sub or alternative

I have a mysql field set as "TIME", and I would like to check against that time, using time as well.
I will have a cron run every 10 minutes, where my clients can set reminder messages to be sent out every day at any time of the day or so.
Using the following query does not give me the fields that are from the last 9 minutes:
SELECT `id` FROM `sms_reminders` WHERE (`option` = "weekdays" OR `option_days` LIKE "%4%") AND `time` < DATE_SUB(NOW(), INTERVAL 9 MINUTE)
However, this is giving me mixed results.
My time field is set at 09:50:00 (Time of running the query is 09:53:00).
Thanks.
make few changes in your code use > this instead of <
After altering the query, I flipped the < to > and it is now working.

update column if date is older than 30 days

I have a table where I have column name 'date' and another column with the name 'status' if I use a cron job to run everyday and check if the date is older than 30 day then to change the status to expired, what code do I need to run using mysqli and php.
MySQL event scheduler is more better.
Start MySQL event scheduler by executing following query in PhpMyAdmin or MySQL command prompt.
Enter this.
SET GLOBAL event_scheduler = 1;
This will update your table.
CREATE EVENT newEvent
ON SCHEDULE EVERY 1 DAY
DO
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY;
Try this:
UPDATE table SET status="expired" where YOURDATEFIELD < date_sub(now(), interval 1 month);
Use INTERVAL in your UPDATE query..
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY

How to auto update a record in database?

I have a jobs table in which I inserts new jobs.
This table also contains the job post date.
By default the job status is open when a new insertion take place.
Now I want to change the status of the job from open to close when the jobs becomes older than 30 days.
How will I do this?
Try creating a event which runs every day like below
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
UPDATE my_table SET status ='closed'
WHERE post_date > DATE_ADD(now(), INTERVAL -30 DAY)
AND status='open'
-- Update Changed syntax
CREATE EVENT myevent
ON SCHEDULE EVERY 24 HOUR
DO
UPDATE my_table SET status ='closed'
WHERE post_date > DATE_ADD(now(), INTERVAL -30 DAY)
AND status='open'
Use a cron job to handle this. http://en.wikipedia.org/wiki/Cron
If you can't handle crons, you could do a "poor man's cron". Means the actual process that updates all of your jobs records takes place when someone visits your page (with additional checks when the last run was). If you're doing it the "poor" way I suggest to fire off another thread to keep your site responsible.

Categories