Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3988436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:15:06+00:00 2026-05-20T06:15:06+00:00

I would like to create an event in MySQL in order to change a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:15:07+00:00Added an answer on May 20, 2026 at 6:15 am

    What is the value in active.time_stamp where active.user_id = 1?

    Also, I think that your statement might be written more simply.

    1. If time_stamp in fact contains timestamps then you don’t need the call to TIMESTAMP().

    2. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a daily event, that is activated with a button
Possible Duplicate: Create event handler for OnScroll for web browser control I would like
I would like to create a method that takes an event as an argument
i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a json object to send as a post array,
I would like to create set of strings and below is the only limitation.
I would like to create a class that runs something (a runnable) at regular
I would like to create a virtual host in apache2, but I want it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.