删除时间戳早于x天的所有行

时间:2023-01-15 10:27:46

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 

2 个解决方案

#1


48  

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

#2


43  

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

Replace N with your day count

将N替换为您的日数

#1


48  

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

#2


43  

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

Replace N with your day count

将N替换为您的日数