[英]I want to fetch the data where I will put the date and I will get data from last three years to the entered date
想象一下,如果我输入 2022 年 1 月 27 日的日期,那么我应该得到从 2019 年 1 月 26 日到 2022 年 1 月 27 日的所有记录。
有几种方法可以做到这一点。 一个警告是您如何定义三年:
select '2022-01-27'::date - interval '3 years';
?column?
---------------------
2019-01-27 00:00:00
或者你有它:
2019-01-26 to 2022-01-27
。
使用您的定义:
SELECT * FROM some_table WHERE date_fld BETWEEN '2019-01-26' and '2022-01-27';
--Using a date range
select * from some_table where date_fld <@ daterange('2019-01-26', '2022-01-27', '[]');
t
--or
select * from some_table where date_fld <@ daterange('2019-01-26', '2022-01-28');
t
默认情况下,日期范围在上限上是排他的,这就是为什么您需要包含'[]'
或将上限提高一个日期(这实际上是'[]'
所做的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.