本文介绍了MySQL:删除超过10分钟的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表中有一个时间戳记字段。如何删除超过10分钟的记录?
尝试这个:
DELETE FROM locks WHERE time_created< DATE_SUB(CURRENT_TIME(),INTERVAL 10 MINUTE)没有工作。我做错了什么?
编辑:我使用这段代码:
SELECT time_created,CURRENT_TIMESTAMP,TIMESTAMPDIFF(MINUTE,time_created,CURRENT_TIMESTAMP)FROM locks但奇怪的是,给出错误的结果
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解决方案
如果time_created是一个unix时间戳(int),你应该可以使用这样的东西:
code> DELETE FROM locks WHERE time_created< (UNIX_TIMESTAMP() - 600);(600秒= 10分钟 - 明显)
否则(如果time_created是mysql时间戳),您可以尝试以下方式:
DELETE FROM locks WHERE time_created< (NOW() - INTERVAL 10 MINUTE)
I have a timestamp field in my table. How do I delete records older than 10 minutes old?
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 locksBut 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解决方案
If time_created is a unix timestamp (int), you should be able to use something like this:
DELETE FROM locks WHERE time_created < (UNIX_TIMESTAMP() - 600);(600 seconds = 10 minutes - obviously)
Otherwise (if time_created is mysql timestamp), you could try this:
DELETE FROM locks WHERE time_created < (NOW() - INTERVAL 10 MINUTE)
更多推荐
MySQL:删除超过10分钟的所有行
发布评论