如何使用条件截断postgreSQL表

如何使用条件截断postgreSQL表,postgresql,Postgresql,我试图用一些条件截断PostgreSQL表 截断表中的所有数据,只保留过去6个月的数据 为此,我写了这个查询 select distinct datecalcul from Table where datecalcul > now() - INTERVAL '6 months' order by datecalcul asc 如何添加truncate子句?truncate不支持WHERE条件。您必须使用DELETE语句 delete from the_table whe

我试图用一些条件截断PostgreSQL表

  • 截断表中的所有数据,只保留过去6个月的数据
为此,我写了这个查询

select distinct  datecalcul
from Table   
where datecalcul  > now() - INTERVAL '6 months' 
order by  datecalcul asc

如何添加truncate子句?

truncate
不支持WHERE条件。您必须使用
DELETE
语句

delete from the_table
where ...


如果您想基于时间戳高效地清除旧的(“过期”)行,可以考虑。然后,您可以删除旧分区。

非常感谢!仅保留最后6个月的内部案例是否正确?有更好的方法吗?@Kakoo16:您可以使用分区快速删除旧行,但您应该为此提出一个新问题。好的,谢谢!间隔条款中的“月”和“月”之间有区别吗?我真的不知道如何让6个月的数据和删除其他数据
delete from Table where d_datecalcul>now()-INTERVAL“6个月”
这是正确的查询吗?