Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

mysql - How to update table after a certain time interval

How can I update a table after some time interval when a condtion is matched?

tb_contest
id   contest_id   name   is_expire
1    101          new    0
2    102          old    0

tb_answer
contest_id   answer_id   date
101          1           2012-02-02
101          2           2012-09-14
102          5           2012-06-01

I need to update tb_contest after some condition was met and make is_expire=1 after 2 days on basis of the last answer received i:e 2012-03-14, so the tb_contest should be updated on 2012-09-16.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use MySQL's event scheduler:

CREATE EVENT expire_contests
  ON SCHEDULE EVERY DAY
     STARTS CURRENT_DATE
  DO UPDATE tb_contest JOIN (
       SELECT   contest_id, MAX(date) AS latest
       FROM     tb_answer
       GROUP BY contest_id
     ) t USING (contest_id)
     SET    tb_contest.is_expire = 1
     WHERE  tb_contest.is_expire <> 1
        AND t.latest <= CURRENT_DATE - INTERVAL 2 DAY

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...