MySQL 删除某个日期之前的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8359457/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:55:03  来源:igfitidea点击:

Deleting records before a certain date

mysqldatetimedelete-row

提问by Hard worker

How would I go about deleting all records from a MySQL table from before a certain date, where the date column is in DATETIME format?

我将如何从某个日期之前从 MySQL 表中删除所有记录,其中日期列采用 DATETIME 格式?

An example datetime is 2011-09-21 08:21:22.

一个示例日期时间是2011-09-21 08:21:22.

回答by Michael Mior

DELETE FROM table WHERE date < '2011-09-21 08:21:22';

回答by Maddy

This helped me delete data based on different attributes. This is dangerous so make sure you back up database or the table before doing it:

这帮助我根据不同的属性删除数据。这很危险,因此请确保在执行此操作之前备份数据库或表:

mysqldump -h hotsname -u username -p password database_name > backup_folder/backup_filename.txt

Now you can perform the delete operation:

现在您可以执行删除操作:

delete from table_name where column_name < DATE_SUB(NOW() , INTERVAL 1 DAY)

This will remove all the data from before one day. For deleting data from before 6 months:

这将删除一天之前的所有数据。删除 6 个月前的数据:

delete from table_name where column_name < DATE_SUB(NOW() , INTERVAL 6 MONTH)

回答by Harano Prithibi

To show result till yesterday

显示结果到昨天

WHERE DATE(date_time) < CURDATE()

To show results of 10 days

显示 10 天的结果

WHERE date_time < NOW() - INTERVAL 10 DAY

To show results before 10 days

10天前显示结果

WHERE DATE(date_time) < DATE(NOW() - INTERVAL 10 DAY)

These will work for you

这些对你有用

You can find dates like this

你可以找到这样的日期

SELECT DATE(NOW() - INTERVAL 11 DAY)