sql删除所有超过30天的行 [英] sql delete all rows older than 30 days

查看:79
本文介绍了sql删除所有超过30天的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何删除超过 30 天的所有行有很多问题,但我找不到与我相同的内容,所以我可以修复它

There are a lot of questions on how to delete all rows older than 30 days but i can't find anything same with mine so i can fix it

我需要删除一些超过 30 天的消息记录,带有日期的列名为 sentOn,该列中的行看起来像这样 2018-01-12 12:25:00我应该如何格式化查询以从包含 30 天以前的记录的表中删除所有记录?

i need to delete some records of messages that are older than 30 days, the column name with the date is named sentOn and the rows in that column looks like this 2018-01-12 12:25:00 How should i format my query to delete all records from the table containing those that are older than 30 days?

DELETE FROM messages WHERE sentOn < '2018-02-21 00:00:00';

这行得通吗?

上面的查询有效,但速度非常慢,有什么办法可以让它更快?我尝试了 now() 但它给出了函数错误的错误

above query works but very very slowly any way to make it faster? i tried now() but it gives error that the function is wrong

推荐答案

以下代码将删除超过30天的消息记录

The following code will delete the records of messages that are older than 30 days

DELETE FROM messages WHERE sentOn < NOW() - INTERVAL 30 DAY;

MySQL 中的 NOW() 方法用于选择当前日期和时间.INTERVAL 30 DAY 用于从当前日期减去 30 天.上述查询后,您可以使用SELECT 语句检查当前表.谢谢!

The NOW() method in MySQL is used to pick the current date with time. INTERVAL 30 DAY used for subtracting 30 days from the current date. After the above query, you can check the current table using the SELECT statement. Thank you!

这篇关于sql删除所有超过30天的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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