doutouman6245 2016-04-17 06:11
浏览 55
已采纳

如何在超时后重置sql字段

So I have a particular field in my database table which I would like to reset to 0 after a fixed period of time, like e.g 1 week. MY language is php. Is there any way to do it? Please help me Okay, so basically I have a column "activation points". These points increase whenever a user does some sort of activity. But I need to reset the points to 0 every week

  • 写回答

2条回答 默认 最新

  • douchujian8124 2016-04-17 07:53
    关注

    There is no really nice way to do this with PHP.

    You have a few options:

    1. Use MySQL's newly added, built-in, scheduler: Event Scheduler.

    2. Use software from sources other than PHP or MySQL, like cron jobs.

    3. Use a PHP library like PHPScheduler which isn't a true scheduler.


    Option 1 is probably your best bet. It is built into MySQL, and you don't have to worry about messing with any other software that you aren't already using.

    Option 2 is easy to implement, but does involve using another tool other than just PHP and MySQL. You can learn how to set up cron jobs with this post.

    Option 3 is not recommended by me, unless you just absolutely want to use PHP to do this. But you will be limited on the customization of the scheduling. PHPScheduler isn't technically a true scheduler, and you can read why in this post. That post linked is older though, so it will mention that there is no scheduling available in MySQL, but since that post was made, Event Scheduler has been made.


    To use Event Scheduler (Option 1), you should refer to this really really good tutorial and if you have any problems, this documentation.

    Here's a preview of how easy it is to use Event Scheduler:

    CREATE EVENT nazzus_cool_event
    ON SCHEDULE EVERY 1 MINUTE
    STARTS CURRENT_TIMESTAMP
    ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO
       INSERT INTO messages(message,created_at)
       VALUES('Nazzus cool event just happened again!',NOW());
    

    This code will create an event called nazzus_cool_event, and it will occur every 1 minute(s) starting at CURRENT_TIMESTAMP (now), and ending an hour from now. It will insert some data into the messages table.

    It's very simple but you should definitely have a quick look at this great tutorial so that you can see some more of its great features.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c