MySQL:删除超过10分钟的所有行

时间:2010-08-08 07:28:17

标签: mysql datetime

我的表中有一个时间戳字段。如何删除超过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

2 个答案:

答案 0 :(得分:129)

如果time_created是一个unix时间戳(int),你应该能够使用这样的东西:

DELETE FROM locks WHERE time_created < (UNIX_TIMESTAMP() - 600);

(600秒= 10分钟 - 显然)

否则(如果time_created是mysql时间戳),你可以试试这个:

DELETE FROM locks WHERE time_created < (NOW() - INTERVAL 10 MINUTE)

答案 1 :(得分:26)

答案恰好在MYSQL manual itself

"DELETE FROM `table_name` WHERE `time_col` < ADDDATE(NOW(), INTERVAL -1 HOUR)"