简体   繁体   中英

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

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);

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