[英]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
imagine if I put the date 27th Jan 2022 then I should get all the records from 26th Jan 2019 till 27th Jan 2022.想象一下,如果我输入 2022 年 1 月 27 日的日期,那么我应该得到从 2019 年 1 月 26 日到 2022 年 1 月 27 日的所有记录。
There are a couple of ways to do this.有几种方法可以做到这一点。 One caveat is how you are defining three years:一个警告是您如何定义三年:
select '2022-01-27'::date - interval '3 years';
?column?
---------------------
2019-01-27 00:00:00
or as you have it:或者你有它:
2019-01-26 to 2022-01-27
. 2019-01-26 to 2022-01-27
。
Using your definition:使用您的定义:
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
By default date ranges are exclusive on the upper bound, that is why you need to include either the '[]'
or bump the upper bound up one date(which is actually what the '[]'
does).默认情况下,日期范围在上限上是排他的,这就是为什么您需要包含'[]'
或将上限提高一个日期(这实际上是'[]'
所做的)。
For more information on ranges Ranges and Range operators有关范围的更多信息范围和范围运算符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.