php 删除所有超过 5 天的行

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

Deleting all rows older than 5 days

phpmysqlsql

提问by Abhi

I want to delete all rows older than 5 days, in my table.

我想删除表中超过 5 天的所有行。

My table have a createdOn column of type date can u suggest me the query.

我的表有一个日期类型的 createdOn 列,您可以向我建议查询吗。

I m using mySql database

我正在使用 mySql 数据库

now i wrote folloiwng query

现在我写了以下查询

SELECT * from `user_games` where created_on >= DATE_SUB(created_on, INTERVAL 5 DAY)

to see the user_games which are five days old

查看五天前的 user_games

but always i m getting same result even when i run query for Invterval 1 Day or Interval 5 day

但即使我运行查询 Invterval 1 Day 或 Interval 5 day 也总是得到相同的结果

enter image description here

在此处输入图片说明

while today curdate is 2011-5-2

而今天curdate是2011-5-2

回答by Sanjay Mohnani

Try this,

尝试这个,

delete from mytable where datediff(now(), mytable.date) > 5

This would be proper approach to delete.

这将是删除的正确方法。

回答by Harry Joy

Use date_subfunction like:

使用date_sub函数,如:

delete from `myTable` where createdOn<DATE_SUB(curdate(), INTERVAL 5 DAY);

回答by Adriano Carneiro

You want to do this:

你想这样做:

delete from myTable 
where createdOn < CurDate() - 5

回答by Subhod30

DELETE FROM table_name WHERE time< (NOW() - INTERVAL 5 DAYS)

DELETE FROM table_name WHERE time< (NOW() - INTERVAL 5 DAYS)