sql - 查询显示今天在 postgres 中输入的记录(纪元时间)

标签 sql json postgresql

我有一个表格,其中的列如下:

                                                       Table "public.myapp_table1"
   Column   |          Type          |                          Modifiers                           | Storage  | Stats target | Description 
------------+------------------------+--------------------------------------------------------------+----------+--------------+-------------
 id         | integer                | not null default nextval('myapp_database1_id_seq'::regclass) | plain    |              | 
 model_name | character varying(150) | not null                                                     | extended |              | 
 device_id  | character varying(150) | not null                                                     | extended |              | 
 data       | jsonb                  | not null                                                     | extended |              | 

在 json 字段数据中,有一个“时间戳”,其中时间以纪元格式存储(引用 https://www.epochconverter.com/ )

我想显示今天输入的所有行。

我正在尝试查询:(查询 json 数据有点新鲜)

select distinct(device_id) from myapp_table1 where (data->'timestamp')> 1570077000;

其中 1570077000 是今天从上午 10:00 开始的纪元时间。

我收到的错误是:

ERROR:  operator does not exist: jsonb > integer
LINE 1: ...ce_id) from myapp_table1 where (data->'timestamp')> 15700770...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

不知道如何管理“>”符号。尚未掌握文档。

如何解决这个问题?

最佳答案

您需要使用 ->> 来获取文本形式的 JSON 值,而不是 ->,后者返回一个 json 对象。此外,可能需要将文本值转换为(大)整数。

考虑:

where (data->>'timestamp')::bigint > 1570077000

引用:JSON Functions and Operators

关于sql - 查询显示今天在 postgres 中输入的记录(纪元时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58216704/

相关文章:

javascript - 在 PHP 中构建 JSON 并在 <script> 中回显时转义字符

python - JSON 到 iOS 字符串文件

javascript - 检查商店是否营业。 MySQL Node.js 业务逻辑

c# - 从数据库中的二进制文件写入 PDF

java - 将 JSON 转换为 GSON 到 Android

ruby-on-rails - 服务器上长时间运行的进程

postgresql - 使用字母值作为 x 轴在 Grafana 中创建直方图

php - 如何在 WHERE row = (?) SQL php 语句中放置多个值

mysql - SQL:从表中获取日期

node.js - sails postgresql 多对多关联不起作用