Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the items in MYSQL greater than 6 months?

Tags:

date

select

mysql

How do I return items in MYSQL based on the Timestamp column great than six months and delete them? I'd like to be able to delete items in the table that are greater than six months so that the table doesn't keep growing .. what's the exact query for that?

like image 292
PHP_lover Avatar asked Jan 11 '13 17:01

PHP_lover


2 Answers

Here's a query to find all rows older than 6 months based on the value of a timestamp column:

select id
from your_table
where your_timestamp_column <= (now() - interval 6 month);
like image 187
Ike Walker Avatar answered Oct 19 '22 00:10

Ike Walker


Try this:

Using DATEDIFF function:

SELECT * FROM tableName  
WHERE DATEDIFF(CURDATE(), colName) > 180;

DELETE FROM tableName  
WHERE DATEDIFF(CURDATE(), colName) > 180;

Using DATE_SUB function:

SELECT * FROM tableName  
WHERE colName < DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

DELETE FROM tableName  
WHERE colName < DATE_SUB(CURDATE(), INTERVAL 6 MONTH);
like image 31
Saharsh Shah Avatar answered Oct 18 '22 22:10

Saharsh Shah