Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting all rows older than 5 days

Tags:

sql

php

mysql

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

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

I m using mySql database

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

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

enter image description here

while today curdate is 2011-5-2

like image 472
Abhi Avatar asked May 02 '11 04:05

Abhi


People also ask

What is the clause to delete all rows from the table?

The truncate command removes all rows of a table.


2 Answers

Try this,

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

This would be proper approach to delete.

like image 50
Sanjay Mohnani Avatar answered Sep 18 '22 13:09

Sanjay Mohnani


Use date_sub function like:

delete from `myTable` where createdOn<DATE_SUB(curdate(), INTERVAL 5 DAY);
like image 26
Harry Joy Avatar answered Sep 19 '22 13:09

Harry Joy