7

我有一个包含两个字段的表:

id(serial), data(jsonb)

在数据中,我有一个 Datetime 字段的记录,该字段存储为 UNIX 时间戳:

{"Device":132,"Datetime": 1434166552,...}

我正在尝试在范围之间进行查询:

SELECT *
FROM trips
WHERE data->>'Datetime' BETWEEN
    EXTRACT(EPOCH FROM date '2014-04-01') AND
    EXTRACT(EPOCH FROM date '2014-04-15' + interval '1 day')
    AND id = 123

信息

ERROR:  operator does not exist: text >= double precision
LINE 3: WHERE data->>'Datetime' BETWEEN

我做错了什么,请云有人帮助我吗?谢谢。

4

1 回答 1

6

->>运算符返回一个 JSON 对象字段text(参见此处)。你需要投射它:

SELECT *
FROM trips
WHERE (data->>'Datetime')::int 
  BETWEEN EXTRACT(EPOCH FROM date '2014-04-01') 
      AND EXTRACT(EPOCH FROM date '2014-04-15' + interval '1 day')
  AND id = 123
于 2015-06-15T01:01:04.057 回答