[英]postgresql search timestamp between dates with java
I have with my partner a project for uni.我和我的搭档有一个大学项目。 one question is to select timestamps between dates.
一个问题是选择日期之间的时间戳。 IE
IE
select * from table where timestamp >= 2020-10-10 and 2020-10-15.
it works fine, but the timestamp doesnt show the 2020-10-15 day.它工作正常,但时间戳没有显示 2020-10-15 天。
is there a good way to include the full day?有没有一个好方法来包括一整天?
I have with my partner a project for uni.我和我的伴侣有一个大学项目。 one question is to select timestamps between dates.
一个问题是选择日期之间的时间戳。 IE
IE
select * from table where timestamp >= 2020-10-10 and 2020-10-15.
it works fine, but the timestamp doesnt show the 2020-10-15 day.它工作正常,但时间戳记未显示2020-10-15。
is there a good way to include the full day?有什么好方法可以包括一整天吗?
First cast "timestamp" to date
and then as @ArvindKumarAvinash said in his answer use inclusive between
.首先将“时间戳”转换为
date
,然后正如@ArvindKumarAvinash 在他的回答中所说的那样使用 inclusive between
. This would include the full day.这将包括一整天。
SELECT * FROM _table WHERE _timestamp::date BETWEEN '2020-10-10' AND '2020-10-15';
Your WHERE expression is wrong.你的 WHERE 表达式是错误的。 When using
>=
and <
you need to repeat the column name.使用
>=
和<
您需要重复列名。
When comparing a timestamp
value (date and time) with a date value (without time), it's usually better to use <
on the upper bound and compare with the "next" day, otherwise you wouldn't catch the values after 00:00:00
将
timestamp
值(日期和时间)与日期值(不带时间)进行比较时,通常最好在上限使用<
并与“下一个”日进行比较,否则您将无法捕捉到00:00:00
之后的值00:00:00
select *
from table
where "timestamp" >= date '2020-10-10'
and "timestamp" < date '2020-10-15' + 1;
This is more efficient than casting "timestamp"
to a date, as this expression can make use of an index on the "timestamp"
column.这比将
"timestamp"
转换为日期更有效,因为此表达式可以使用"timestamp"
列上的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.