Delete all rows with timestamp older than x days

偶尔善良 提交于 2019-11-27 05:19:08

问题


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 

回答1:


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



回答2:


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

Replace N with your day count



来源:https://stackoverflow.com/questions/21206361/delete-all-rows-with-timestamp-older-than-x-days

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!