我在表src
中有以下行,其中一个属性是json数组。
属性:
[{"key": "Tag", "value": "myTagValue"}, {"key": "Brand", "value": "myBrandValue"}]
[{"key": "Tag", "value": "myTagValue"}, {"key": "Brand", "value": "myBrandOtherValue"}, {"key": "Test", "value": "123"}]
如何选择Brand
?
预期输出:
Brand
-----
myBrandValue
myBrandOtherValue
(2 rows affected)
我想使用json_to_recordset(json)
函数,但属性可以有N
不同的值(jsons)。
demo:db<>fiddle
SELECT
elems.value ->> 'value' -- 3
FROM mytable,
jsonb_array_elements(mydata) elems -- 1
WHERE elems.value ->> 'key' = 'Brand' -- 2
- 每个JSON元素提取所有数组元素到一行
- 通过
key = Brand
筛选JSON元素 - 返回
value