How to AUTO update MySQL after timestamp field expierd

前端 未结 1 1614
夕颜
夕颜 2020-12-05 22:08

How do I automate MySQL Update or Insert upon expire timestamp?

So let say timestamp is 2013-06-30 20:10:00 and I would like to auto update MySQL DB upo

相关标签:
1条回答
  • 2020-12-05 23:10

    Use can use for that

    1. Mysql events (IMHO the best candidate)
    2. cron job or Windows Task Scheduler (if you're on Windows platform)

    If you go with option 1 you need to create an event

    CREATE EVENT myevent 
    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR DO 
    UPDATE myschema.mytable 
       SET mycol = mycol + 1;
    

    Use SHOW PROCESSLIST to check if event scheduler is enabled. If it's ON you should see a process "Daemon" by user "event_scheduler". Use SET GLOBAL event_scheduler = ON; to enable the scheduler if it's currently not enabled. More on configuring event scheduler here.

    If you want to see events that you've in your schema

    SHOW EVENTS;
    

    UPDATE Your update statement should look like

    UPDATE online_auctions 
       SET auction_status = 'ENDED' 
     WHERE auction_end_date < NOW();
    

    Here is SQLFiddle demo

    0 讨论(0)
提交回复
热议问题