我在 postgres 中有一个名为 masters 的表。
身份证 | 参考 | 自定义字段 |
---|---|---|
1 | 约翰 | [ { "ID": 17,"NumericValue": 1234},{ "ID":21,"DateValue":"2023-06-14T00:00:00Z"},{ "ID": 24, "BooleanValue" :假} |
2 | 史密斯 | [ { "ID": 17,"NumericValue": 1234},{ "ID":12,"DateValue":"2023-04-05T00:00:00Z"},{ "ID": 15, "ListItemValue" :“印度”} |
自定义字段列数据类型为jsonb。 我想按 custom_fields 列键 DateValue 过滤行。为此,我使用了 json 路径表达式,如Postgres文档
中所述select id,reference,custom_fields_json from master.masters
where custom_fields_json @? '$[*] ? (@.DateValue.datetime() > "2022-06-22T03:09:37Z".datetime())'
但是过滤不起作用。查询不返回任何行。我认为日期格式有问题。在文档中,他们展示了“2015-08-12”日期格式的示例
问题与日期格式有关。 JSON 数据中的日期格式是 ISO 8601 格式,它应该与 PostgreSQL 的 JSON 函数配合良好
请尝试一下这个
SELECT id, reference, custom_fields_json
FROM master.masters
WHERE custom_fields_json @? '$[*] ? (@.DateValue > "2022-06-22T03:09:37Z")';