Postgres:sql 按过滤日期范围查询

Postgres: sql query by filter date range

我有 table 测试,其中包括日期列 potatoeDate。我想通过从该列中按日期查询来获取记录。当我按日期从 Y.AA.AAAA 到 X.AA.AAAA 查询时,总是从 [=20] 获得记录=]Y.AA.AAAA 到 (X-1).AA.AAA。例如,我从 01.10.2017 搜索到 30.10.2017,但我得到了 01-29.10.2017 范围内的记录。

我尝试了我所知道的一切,事件子查询,但没有任何帮助。 我的尝试:

    Select n1.potatoeDate 
    FROM (SELECT potatoeDate from test WHERE  potatoeDate > '2017/05/07'::date) n1
    WHERE  n1.potatoeDate <= '2017/05/08'::date;

    select * 
    from test
    where potatoeDate between '2017/05/07' and '2017/05/08'

    SELECT potatoeDate from test
    where
    potatoeDate >= '2017/05/07' AND
    potatoeDate <= '2017/05/08'

--little hack
    SELECT potatoeDate from test
    WHERE  
    potatoeDate >= '2017/05/07'::date
    AND 
    potatoeDate <= ('2017/05/08'::date)+1;

只有最后一个小 hack 查询有效。 :|

有人可以帮助我吗? :)

我猜 potatoeDate 的类型是 timestamp

当您将 datetimestamp 进行比较时,即使只有一秒钟,timestamp 也会更大(时间更晚)。尝试将 date 转换为 timestamp 并查看它在时间字段中具有 00:00:00 。因此 timestamp 的时间不同于 00:00:00 会更大。

我找到了解决方案(来自@Adam 的信息有所帮助)。 :)

在这种情况下,有必要将 where 子句中的列名转换为 date 类型。当它发生变化时,所有查询 return 预期结果。

示例:

select * 
from test
where potatoeDate::date between '2017/05/07' and '2017/05/08'

SELECT potatoeDate from test
where
potatoeDate::date >= '2017/05/07' AND
potatoeDate::date <= '2017/05/08'