Ahmadhc
Ahmadhc

Reputation: 173

Delete item from database after X days

I am trying to make a Java Thread, this thread have to delete from an MySQL database all the records, older than 7 days.

In my table i have a column that contain the date like this: 2013-10-28 17:00:00 .

And to do this i want to use JDBI library. and my question here if any one could give me and example of the query that i have to write.

I think it should look like this:

h.execute("Delete from  MyTable where date >= (dt.now.dayofmonth() -7)

Upvotes: 0

Views: 1533

Answers (3)

alandarev
alandarev

Reputation: 8635

h.execute("DELETE FROM MyTable WHERE NOW() >= ADDDATE(date, INTERVAL 7 DAY);");

Upvotes: 2

ilintar
ilintar

Reputation: 99

"delete from MyTable where date >= date_sub(now(), interval 7 day)"

Upvotes: 0

Sashi Kant
Sashi Kant

Reputation: 13465

Try this::

h.execute("Delete from  MyTable where DATEDIFF(CURDATE(), dateCOLUMN)>7");

Upvotes: 1

Related Questions