PostgreSQL:如何从今天的日期获取过去 2 年的数据

man*_*lla 7 postgresql

我正在使用 PostgreSQL,需要知道如何从今天开始获取过去 2 年的数据,即 current_date。我的查询正确吗?

select count(*) from table_name where
creation_date >= date_trunc('year', now()) - interval '2' year and
creation_date < date_trunc('year', now());
Run Code Online (Sandbox Code Playgroud)

Adr*_*ver 9

假设您想要从当前日期开始的两年跨度:

select count(*) from table_name where
creation_date >= current_date - interval '2' year and
creation_date < current_date;

Run Code Online (Sandbox Code Playgroud)

使用current_date消除了截断的需要now()