Automatically Changing Each Value of a MySQl column - mysql

I have a column with numerous integer values. How can I code it so that every twenty-four hours each value is subtracted by one until it reaches 0? Till now, I have understood the code for repeating an event every 24 hours but could not find a way to subtract one from each individual value. Thank you!
SET GLOBAL event_scheduler = ON;
SELECT ##event_scheduler;
CREATE EVENT e_store_ts
ON SCHEDULE
EVERY 24 HOURS
DO
UPDATE users set value= '1';

You can use case statement to update only values which are greater than zero
SET GLOBAL event_scheduler = ON;
SELECT ##event_scheduler;
CREATE EVENT e_store_ts
ON SCHEDULE
EVERY 24 HOURS
DO
UPDATE users SET `value`= CASE WHEN `value` > 0 THEN `value` - 1 ELSE `value` END;

Related

MySQL event or trigger to automatically update a value 30 minutes after last modify

I have in my DB (db_test) a table (tb_test) with 3 columns (id, test_field, timestamp_ins, timestamp_mod).
id is the 'primary key' with auto-increment attribute;
test_field is a char(1) that can contains only 2 values ('N' or 'S');
timestamp_ins is a datetime with current timestamp (not updating);
timestamp_mod is a datetime with current timestamp set to ON UPDATE CURRENT_TIMESTAMP().
I would like test_field automatically returns to default value ('N') 30 minutes after last modify of record indicated in timestamp_mod value.
I'm not expert in mysql, so I need help about this.
Is it possible using phpMyAdmin on a XAMPP virtual server?
--UPDATE--
Solved with this syntax:
CREATE EVENT IF NOT EXISTS test_event
ON SCHEDULE EVERY 1 MINUTE
DO
UPDATE ni0y2__test
SET test_field = DEFAULT
WHERE test_field < NOW() - INTERVAL 30 MINUTE
Only one doubt:
can this event make my DB performances worse?
I prefer putting the onus on the reader, not the table:
SELECT IF (timestamp_mod < NOW() - INTERVAL 30 MINUTE,
'N',
test_field) AS funky_field
FROM tb_test;
EVENTs have some overhead. Also, the above method will change at precisely 30 minutes; any attempt at using EVENT will be only approximately 30.
You could "hide" the IF(...) from the users by using a VIEW or Stored Function or a GENERATED column.

Sailsjs table needs to be updated automatically without using any cron

I am using SAILS JS and mysql adapter is being used. I have a model named as User with the following fields ID, USERNAME, EMAIL, ACTIVE_STATUS and CREATED_DATE.
By Default, active_status is set as 0. I want to update the status is 1 when created_date + 3 days is equal on Today.
Kindly suggest any possible ways to do this.
Hope you can use MySQL’s Event Scheduler
Activate it by: SET GLOBAL event_scheduler = ON;
Create event syntax:
CREATE EVENT `event_name`
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
DO BEGIN
-- event body
END;
The schedule can be assigned various settings, e.g.
Run once on a specific date/time:
AT ‘YYYY-MM-DD HH:MM.SS’
e.g. AT ‘2011-06-01 02:00.00’
Run once after a specific period has elapsed:
AT CURRENT_TIMESTAMP + INTERVAL n [HOUR|MONTH|WEEK|DAY|MINUTE]
e.g. AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
Run at specific intervals forever:
EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE]
e.g. EVERY 1 DAY
Run at specific intervals during a specific period:
EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE] STARTS date ENDS date
e.g. EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK ENDS ‘2012-01-01 00:00.00’
Example:
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 24 HOURS -- run every 24 hours
DO
UPDATE myschema.users set active_status = 1
Refer: http://dev.mysql.com/doc/refman/5.5/en/create-event.html

How to update column value after some time interval

I need to update a particular column value from Y to N after every 3 minutes.
Table Structure
`u_id` int(11) NOT NULL,
`status` varchar(11) NOT NULL
How can i change status N after 3 mins for each row in phpmyadmin
It seems like you are looking for something called EVENTS in MySQL.
You may try following steps to achieve.
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 180 SECOND -- run every 180 secs (3 Min)
DO
UPDATE myschema.youtable set mycolumn='N' -- update this table
To see the events that created
SHOW EVENTS;
For more detail : Schedule SQL Query to execute on specific time interval
You can also create a corn job to run every 3 minutes, which is the most standard way of doing repetitive work.
Refer here which gives an easy explanation about how to create corn job.
The corn expression to run every 3 minutes is: 0 0/3 * 1/1 * ? *

MySQL - Change a row value using a timer

I have been developing ban system with temporary banning system and I'm looking if there's anyway to update a row to specific value using a timer within MySQL. For instance,
I need my field banned to be set to 0 where the rowid is X.
UPDATE mytable SET banned=0 WHERE rowid=X
- But this query has to be done within X minutes or seconds specified, like if we could delay it.
Banning a user temporarily for 60 seconds, query sent at that time:
"UPDATE mytable SET banned=1, unbanon=NOW()+60 WHERE rowid=X".
Could I possibly get MySQL to automatically set banned variable to 0 after 60 seconds has passed?
Thank you.
Set a cron job to run a query every minute that will do something like :
UPDATE mytable SET banned=0 WHERE unbanon <= NOW() AND banned=1

Update a value using MySQL Events

I would like to create an event in MySQL in order to change a value in a table. The event checks whether a date (particularly, the TIME() part of a TIMESTAMP field) is more than the current date. Obviously, I have a time_stamp and an active column in a table named 'active'. This is what I have
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT #time:= time_stamp FROM active WHERE user_id = 1;
SELECT #time2:= TIMESTAMP(#time);
SELECT #active:=TIMEDIFF(#time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=if(TIME_TO_SEC(TIME(#active))>=0,1,0) WHERE user_id=1;
As far as I have seen, the SELECT part with the variables works fine, that is, TIME_TO_SEC(TIME(#active)) looks like a regressive count starting with 15 (provided I updated the 'time_stamp' field properly).
The intended behavior is that when TIME_TO_SEC(TIME(#active)) reach 0, the UPDATE query would change the value of the 'active' field from 1 to 0 (the default value is 1). However, it doesn't do anything.
UPDATE: To be precise, sometimes it changes the value to 0 but when TIME_TO_SEC(TIME(#active)) is still positive.
SECOND UPDATE: I recently tried this one:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT #time:= time_stamp FROM active WHERE user_id = 1;
SELECT #time2:= TIMESTAMP(#time);
SELECT #active:=TIMEDIFF(#time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=0 WHERE user_id=1 AND TIME_TO_SEC(TIME(#active))>=0;
which didn't work either.
THIRD UPDATE: I used the suggestion by Adam and I even if it doesn't work as expected, it certainly changes the value 'active' to 0, immediately after I start the event. Ideas?
SOLUTION: Thanks to Adam, I used his code with an IF statement:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
Do
UPDATE active AS t
SET t.active=IF(TIME_TO_SEC(TIMEDIFF(
t.time_stamp, DATE_ADD(NOW(),
INTERVAL -15 SECOND)))>=0,1,0)
WHERE user_id=1
which it works as intended. I don't why his suggestion didn't work, though.
Where did I make a mistake?
Of course, another approach to get this functionality would be greatly appreciated.
Thanks in advance.
What is the value in active.time_stamp where active.user_id = 1?
Also, I think that your statement might be written more simply.
If time_stamp in fact contains timestamps then you don't need the call to TIMESTAMP().
Remove TIME() function from UPDATE query because TIMEDIFF(expr1,expr2) returns expr1 – expr2 expressed as a time value.
So this is how I wrote your statement:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
UPDATE active t
SET t.active = 0
WHERE t.user_id = 1
AND TIME_TO_SEC(
TIMEDIFF(
t.time_stamp, DATE_ADD(NOW(),INTERVAL -15 SECOND)
)
)>=0;
I've tested that when the value in active.time_stamp is greater than now, this event sets the value in active.active to 0.
If that still isn't working you might also try something very basic like:
CREATE EVENT update_status_test
ON SCHEDULE EVERY 1 SECOND
DO
UPDATE active t
SET t.active = 0
WHERE t.user_id = 1;
And if that still isn't working then make sure the event scheduler is actually running. There are several ways to start it, this is one: SET GLOBAL event_scheduler = 1
Other notes:
This may not matter now, but as time goes on you will eventually run into the upper-limit of the TIME type in MySQL.