简体   繁体   中英

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

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.

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)

We can test expressions in a simple SELECT statement, eg

SELECT DATE_FORMAT( NOW() + INTERVAL -10 DAY , '%Y-%m-%dT00:00:00') AS dt 

Then we can use that expression in a WHERE clause

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; 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".

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 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. Just use date() :

DELETE FROM myTable
WHERE DATE(createdDate) < NOW() - INTERVAL 10 DAY;

There is no To_date() function in MySQL. You will need to use Str_To_Date() function instead:

DB Fiddle DEMO

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
  • %m Month name as a numeric value (00 to 12)
  • %d Day of the month as a numeric value (01 to 31)
  • %T Time in 24 hour format (hh:mm:ss)
  • There is an additional 'T' substring in your datetime string. That is represented directly by using 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. 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 天;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM