MySQL 删除所有时间戳早于 x 天的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21206361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete all rows with timestamp older than x days
提问by Khaleal
I want to delete all the rows with timestamp older than 180 days from a specific table in my database.
我想从数据库中的特定表中删除时间戳早于 180 天的所有行。
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.
但这删除了所有行,而不仅仅是超过 6 个月的行。
I have a column in on_search table called search_date and contains the time when that row was created.
我在 on_search 表中有一个名为 search_date 的列,其中包含创建该行的时间。
search_id search_term search_date
660779 car games 1390052553
回答by JSR
DELETE FROM on_search
WHERE search_date < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 180 DAY))
回答by Ali MasudianPour
DELETE FROM on_search WHERE search_date < NOW() - INTERVAL N DAY
Replace N with your day count
用您的天数替换 N