简体   繁体   English

PostgreSQL (Heroku) 中的时间/日期时间比较

[英]Time/datetime Comparison in PostgreSQL (Heroku)

I'm building an app for Heroku (Postgres) but for various reasons, developing it locally using MySQL.我正在为 Heroku (Postgres) 构建一个应用程序,但出于各种原因,使用 MySQL 在本地开发它。 I've hit a roadblock concerning something that my MySQL dev environment takes with no trouble but that the heroku Postgres environment hangs up on.我遇到了一个障碍,因为我的 MySQL 开发环境没有任何问题,但 heroku Postgres 环境挂断了。 Here is the log stream from Heroku:这是来自 Heroku 的日志流:

2012-02-07T10:00:00+00:00 app[web.1]: Report Load (2.5ms)  SELECT id, datetime_utc FROM "reports" WHERE (datetime_utc = '2012-02-07 10:00:00.000000') AND (datetime_utc > spot_sunrise_utc AND datetime_utc > spot_sunset_utc) ORDER BY datetime_utc ASC LIMIT 500
2012-02-07T10:00:00+00:00 app[web.1]: PGError: ERROR:  operator does not exist: timestamp without time zone > time without time zone
2012-02-07T10:00:00+00:00 app[web.1]: LINE 1: ...EEN 0.4573170731707317 and 200) AND (datetime_utc > spot_sun...
2012-02-07T10:00:00+00:00 app[web.1]:                                                              ^
2012-02-07T10:00:00+00:00 app[web.1]: HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

The datetime_utc field is a real datetime whereas the spot_sunrise / sunset_utc fields are just time values. datetime_utc字段是一个真实的datetimespot_sunrise / sunset_utc字段只是时间值。

What is causing this, and how do I work around it?这是什么原因造成的,我该如何解决?

To alleviate the type incompatibility, cast the timestamp value datetime_utc to time :为了缓解类型不兼容,将timestampdatetime_utctime

SELECT id, datetime_utc
FROM   reports
WHERE  datetime_utc = '2012-02-07 10:00'
AND    datetime_utc::time > spot_sunrise_utc 
AND    datetime_utc::time < spot_sunset_utc  -- you mean <, right?
ORDER  BY datetime_utc  -- noise
LIMIT  500;

See:看:

The added ORDER BY datetime_utc is just noise after filtering with WHERE datetime_utc = '2012-02-07 10:00' .添加的ORDER BY datetime_utc只是使用WHERE datetime_utc = '2012-02-07 10:00'过滤后的噪音。

Aside 1: It is generally a good idea to develop with the same DB locally as you deploy with.旁白 1:在本地使用与部署相同的数据库进行开发通常是个好主意。 So it is a bad idea to use MySQL locally.所以在本地使用 MySQL 是一个坏主意。

Aside 2: The name of the database is PostgreSQL or Postgres for short.旁白2:数据库名称为PostgreSQL或Postgres。 No such thing as "postgre".没有“postgre”这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM