我创建了一个名为“delivery”的表,其中一个字段是一个 JSON 数组。
CREATE TABLE delivery
(
id text NOT NULL,
date timestamp with time zone NOT NULL,
items json NOT NULL,
CONSTRAINT delivery_pkey PRIMARY KEY (id)
)
这里是示例数据:
"1", "2012-01-08 20:54:38.541989+00", "[ {"id":1, "qty": 10 }, {"id":2, "qty": 300}]"
"2", "2012-01-08 22:44:10.016285+00", "[ {"id":1, "qty": 200}]"
如何获取“items”字段中“id”等于 1 的行?
最佳答案
你可以像这样使用子查询和 json 函数来做到这一点:
SELECT * FROM delivery
WHERE '1' in (
select a->>'id' from json_array_elements(items) a
)
这是 SQL fiddle一起玩。
关于json - 如何在 PostgreSQL 9.3 中查询 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24733196/