json - Postgres数组json最近日期
问题描述
我有一个这样的json数组:
[
{
"date": "26/11/2020 23:27",
"note": "test1"
},
{
"date": "22/11/2020 22:59",
"note": "test2"
},
{
"date": "18/11/2020 17:08",
"note": "test3"
}
]
我想采用具有最新数据的元素。
我获取第一个元素的旧查询是这样的:
(notes\:\:json->0)\:\:json->>'note' as note,
(notes\:\:json->0)\:\:json->>'date' as date_note
解决方案
SELECT
elem.value ->> 'date' as thedate,
elem.value ->> 'note' as note
FROM t,
json_array_elements(data) elem -- 1
WHERE id = 4123
ORDER BY to_timestamp(elem ->> 'date', 'DD/MM/YYYY HH24:MI') DESC -- 2
LIMIT 1 -- 3
- 将所有数组元素提取到一行
- 从字段中读取日期时间字符串
date
,转换为时间戳,并使用它首先对具有最新时间戳的所有数组元素进行排序 - 只需返回第一个(= 最近的)数组元素。
推荐阅读
- angular - @ViewChild 和 @ViewChildren 总是返回未定义,即使没有使用 *ngIf
- python - PyQt 连接中的参数
- android - Android webView Whatsapp Web 加载移动站点
- video - DirectShow 的 H.264 编码器过滤器
- html - 升级到引导程序 4 后引导程序 3 图标丢失
- linux - 我想从文件中提取完整的一行。这必须在函数内部完成
- android - 如何在 android studio 的 textview 中启用 c 代码的语法高亮显示?
- laravel - Laravel router-link 仅在第一次工作
- git - WebStorm:推送 Git 分支时抛出错误代码 32708
- google-chrome - 在本地开发 HTTP2 站点时如何避免“您的连接不是私有的”屏幕?