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?
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With