带有时区的 Postgres 错误查询结果
Postgres wrong query result with timezones
我的 Table 看起来像这样:
库存
inventory | creationDate
{} | 2017-04-27 14:15:15.25+02
{} | 2017-04-27 13:03:02.205+02
{} | 2017-04-27 13:03:01.766+02
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
查询:
SELECT * FROM "Inventory"
WHERE "registerTillInventoryId" = 1
AND "creationDate" <= '2017-04-27 12:02:38.000 +00:00'
ORDER BY "creationDate" DESC;
结果:
inventory | creationDate
{} | 2017-04-27 13:03:02.205+02
{} | 2017-04-27 13:03:01.766+02
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
我的问题是为什么我从
获取数据
13:03:02.205+02 and 13:03:01.766+02
我的查询结果应该是
inventory | creationDate
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
'2017-04-27 12:02:38.000 +00:00'
是 '2017-04-27 14:02:38.000 +00:02'
,
所以它 returns 除了 '2017-04-27 14:15:15.25+02'
之外的所有值,因为它是唯一不符合 "creationDate" <= '2017-04-27 12:02:38.000 +00:00'
的值
没错。示例:
t=# with a as (select '2017-04-27 12:02:38.000 +00:00' at time zone 'utc' ts)
select ts, ts at time zone 'gmt+2' from a;
ts | timezone
---------------------+------------------------
2017-04-27 12:02:38 | 2017-04-27 14:02:38+00
(1 row)
Time: 0.480 ms
为什么不 "creationDate" <= '2017-04-27 12:02:38.000'
来避免混合 tz?..
好的,谢谢你的帮助。我在Vao Tsun的评论的帮助下解决了问题。
我没有描述的是我试图从 12:02 +00:00
得到 13:02
我真的不需要时区。
所以如果我查询
creationDate < '2017-04-27 13:02:38.000'
直接。没有时区,我得到了正确的结果。
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
...
我的 Table 看起来像这样:
库存
inventory | creationDate
{} | 2017-04-27 14:15:15.25+02
{} | 2017-04-27 13:03:02.205+02
{} | 2017-04-27 13:03:01.766+02
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
查询:
SELECT * FROM "Inventory"
WHERE "registerTillInventoryId" = 1
AND "creationDate" <= '2017-04-27 12:02:38.000 +00:00'
ORDER BY "creationDate" DESC;
结果:
inventory | creationDate
{} | 2017-04-27 13:03:02.205+02
{} | 2017-04-27 13:03:01.766+02
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
我的问题是为什么我从
获取数据13:03:02.205+02 and 13:03:01.766+02
我的查询结果应该是
inventory | creationDate
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
'2017-04-27 12:02:38.000 +00:00'
是 '2017-04-27 14:02:38.000 +00:02'
,
所以它 returns 除了 '2017-04-27 14:15:15.25+02'
之外的所有值,因为它是唯一不符合 "creationDate" <= '2017-04-27 12:02:38.000 +00:00'
没错。示例:
t=# with a as (select '2017-04-27 12:02:38.000 +00:00' at time zone 'utc' ts)
select ts, ts at time zone 'gmt+2' from a;
ts | timezone
---------------------+------------------------
2017-04-27 12:02:38 | 2017-04-27 14:02:38+00
(1 row)
Time: 0.480 ms
为什么不 "creationDate" <= '2017-04-27 12:02:38.000'
来避免混合 tz?..
好的,谢谢你的帮助。我在Vao Tsun的评论的帮助下解决了问题。
我没有描述的是我试图从 12:02 +00:00
得到 13:02我真的不需要时区。 所以如果我查询
creationDate < '2017-04-27 13:02:38.000'
直接。没有时区,我得到了正确的结果。
{} | 2017-04-27 13:02:19.8+02
{} | 2017-04-27 12:35:52.12+02
...