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

编程入门 行业动态 更新时间:2024-10-24 11:15:51
本文介绍了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 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

解决方案

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分钟的所有行

本文发布于:2023-11-26 12:53:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!