我正在使用 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)
假设您想要从当前日期开始的两年跨度:
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()。
| 归档时间: |
|
| 查看次数: |
11231 次 |
| 最近记录: |