[英]SQL delete all rows with date older than x days
I have a table like below and I am trying delete all rows with createdDate older than 10 days form current date, like last 3 streams to be deleted form table我有一个如下表,我正在尝试从当前日期中删除 createdDate 超过 10 天的所有行,例如要从表中删除的最后 3 个流
ID Name createdDate(string)
76 Stream1 2018-10-18T00:00:00
70 Stream2 2018-10-17T00:00:00
50 Stream3 2018-10-03T00:00:00
32 Stream4 2018-09-22T00:00:00
21 Stream5 2018-09-21T00:00:00
I tried below queried but didn't work, can some one help我试过下面查询但没有用,有人可以帮忙吗
DELETE FROM myTable WHERE TO_DATE(createdDate) < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 DAY))
DELETE FROM myTable WHERE TO_DATE(createdDate) < NOW()- INTERVAL 10 DAY;
For performance, to allow MySQL to use a range scan (using an index with createdDate
as the leading column), I would reference the bare column in the WHERE
clause.为了性能,为了允许 MySQL 使用范围扫描(使用以
createdDate
作为前导列的索引),我将在WHERE
子句中引用裸列。
I would do any required manipulation on the literal side of the comparison, to come up with a string that we can compare the bare column to.我会在比较的文字方面进行任何必要的操作,以得出一个字符串,我们可以将其与裸列进行比较。
I'd write a SELECT statement first, to verify that it's working (before we run a DELETE and find out that it deletes more than we expected)我会先写一个 SELECT 语句,以验证它是否有效(在我们运行 DELETE 并发现它删除的比我们预期的更多之前)
We can test expressions in a simple SELECT statement, eg我们可以在一个简单的 SELECT 语句中测试表达式,例如
SELECT DATE_FORMAT( NOW() + INTERVAL -10 DAY , '%Y-%m-%dT00:00:00') AS dt
Then we can use that expression in a WHERE clause然后我们可以在 WHERE 子句中使用该表达式
SELECT t.*
FROM myTable t
WHERE t.createddate < DATE_FORMAT( NOW() + INTERVAL -10 DAY , '%Y-%m-%dT00:00:00')
ORDER BY t.createddate DESC
This expression is using midnight '00:00:00'
as the time component;此表达式使用午夜
'00:00:00'
作为时间分量; there are other expressions that would achieve an equivalent result.还有其他表达式可以达到相同的结果。
WHERE t.createddate < DATE_FORMAT( DATE(NOW()) + INTERVAL -10 DAY , '%Y-%m-%dT%T')
We really need to narrow down and be specific what is meant by "older than 10 days than current date".我们确实需要缩小范围并具体说明“比当前日期早 10 天”的含义。
Once we are sure that the query is returning the rows we want to delete, I would convert this into a DELETE statement by replacing the SELECT keyword, and omitting the ORDER BY一旦我们确定查询正在返回我们想要删除的行,我将通过替换 SELECT 关键字并省略 ORDER BY 将其转换为 DELETE 语句
SELECT t.*
FROM myTable t
WHERE t.createddate < DATE_FORMAT( NOW() + INTERVAL -10 DAY , '%Y-%m-%dT00:00:00')
I don't think to_date()
is a function in MySQL.我不认为
to_date()
是 MySQL 中的函数。 Just use date()
:只需使用
date()
:
DELETE FROM myTable
WHERE DATE(createdDate) < NOW() - INTERVAL 10 DAY;
There is no To_date()
function in MySQL. MySQL 中没有
To_date()
函数。 You will need to use Str_To_Date()
function instead:您将需要改用
Str_To_Date()
函数:
DELETE FROM myTable
WHERE STR_TO_DATE(createdDate, '%Y-%m-%dT%T') < NOW()- INTERVAL 10 DAY;
Details:细节:
%Y
Year as a numeric, 4-digit value %Y
年份为 4 位数字值%m
Month name as a numeric value (00 to 12) %m
月份名称为数值(00 到 12)%d
Day of the month as a numeric value (01 to 31) %d
以数值表示的月份中的第几天(01 到 31)%T
Time in 24 hour format (hh:mm:ss) %T
时间为 24 小时格式 (hh:mm:ss)T
T
表示i think date conversion need in both side of comparison我认为比较双方都需要日期转换
DELETE FROM myTable
WHERE DATE(createdDate) < date(NOW()- INTERVAL 10 DAY);
There is no function TO_DATE
in MySQL. MySQL 中没有函数
TO_DATE
。 You can try this query instead:你可以试试这个查询:
DELETE FROM myTable WHERE DATE(createDate) < DATE_SUB(NOW(), INTERVAL 10 DAY);
从 myTable 中删除 t WHERE t.createdDate< NOW() - 间隔 10 天;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.