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 inte开发者_C百科nded 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 toTIMESTAMP()
.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.
精彩评论