SQL条目在24小时后过期 [英] SQL entries that expire after 24 hours

查看:98
本文介绍了SQL条目在24小时后过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个表,其中将条目插入PHP和MySQL后24小时过期.

I want to make a table where the entries expire 24 hours after they have been inserted in PHP and MySQL.

理想情况下,每次用户与我的服务器进行交互时,我都希望运行一个删除过程",这将删除旧条目.由于这种情况比较常见,因此您应该删除大量数据,因此只需几毫秒.

Ideally I want to run a "deleting process" every time a user interacts with my server, that deletes old entries. Since this is more frequent you should it will not have large amounts of data to delete so it should only take a few milliseconds.

我给每个条目一个日期/时间附加值.

I have given each entry a date/time added value.

我该怎么做?

推荐答案

您可以使用MySQL的事件计划程序:

You could use MySQL's event scheduler either:

  • 在此类记录过期时自动删除:

  • to automatically delete such records when they expire:

CREATE EVENT delete_expired_101
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 24 HOUR DO
DELETE FROM my_table WHERE id = 101;

  • 定期自动清除所有过期记录:

  • to run an automatic purge of all expired records on a regular basis:

    CREATE EVENT delete_all_expired
    ON SCHEDULE EVERY HOUR DO
    DELETE FROM my_table WHERE expiry < NOW();
    

  • 这篇关于SQL条目在24小时后过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆