Delete all rows with timestamp older than x days

MysqlSql Delete

Mysql Problem Overview


I want to delete all the rows with timestamp older than 180 days from a specific table in my database.

I've tried the this:

DELETE FROM on_search WHERE search_date < DATE_SUB(NOW(), INTERVAL 180 DAY);

But that deleted all the rows and not only the rows older than 6 months.

I have a column in on_search table called search_date and contains the time when that row was created.

search_id   search_term    search_date 
660779      car games      1390052553 

Mysql Solutions


Solution 1 - Mysql

DELETE FROM on_search 
WHERE search_date < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 180 DAY))

Solution 2 - Mysql

DELETE FROM on_search WHERE search_date < NOW() - INTERVAL N DAY

Replace N with your day count

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKhalealView Question on Stackoverflow
Solution 1 - MysqlDeveshView Answer on Stackoverflow
Solution 2 - MysqlAli MasudianPourView Answer on Stackoverflow