MySQL:删除所有早于 10 分钟的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3433465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: Delete all rows older than 10 minutes
提问by HyderA
I have a timestamp field in my table. How do I delete records older than 10 minutes old?
我的表中有一个时间戳字段。如何删除超过 10 分钟的记录?
Tried this:
试过这个:
DELETE FROM locks WHERE time_created < DATE_SUB( CURRENT_TIME(), INTERVAL 10 MINUTE)
Didn't work. What am I doing wrong?
没用。我究竟做错了什么?
EDIT: I used this code:
编辑:我使用了这个代码:
SELECT time_created, CURRENT_TIMESTAMP, TIMESTAMPDIFF( MINUTE, time_created, CURRENT_TIMESTAMP ) FROM locks
But oddly, this gives the wrong result too
但奇怪的是,这也给出了错误的结果
time_created CURRENT_TIMESTAMP TIMESTAMPDIFF( MINUTE, time_created, CURRENT_TIMESTAMP ) 2010-08-01 11:22:29 2010-08-08 12:00:48 10118 2010-08-01 11:23:03 2010-08-08 12:00:48 10117
回答by Ivar Bonsaksen
If time_created is a unix timestamp (int), you should be able to use something like this:
如果 time_created 是一个 unix 时间戳 (int),你应该能够使用这样的东西:
DELETE FROM locks WHERE time_created < (UNIX_TIMESTAMP() - 600);
(600 seconds = 10 minutes - obviously)
(600 秒 = 10 分钟 - 显然)
Otherwise (if time_created is mysql timestamp), you could try this:
否则(如果 time_created 是 mysql 时间戳),你可以试试这个:
DELETE FROM locks WHERE time_created < (NOW() - INTERVAL 10 MINUTE)
回答by Arno
The answer is right in the MYSQL manual itself.
答案在MYSQL 手册本身中是正确的。
"DELETE FROM `table_name` WHERE `time_col` < ADDDATE(NOW(), INTERVAL -1 HOUR)"