我有問題,更新的postgree jsonb類型,PostgreeSQL查詢ň操縱JSON數據
這是我的例子JSON
{
"total": 2,
"products":[
{"name": "guitar", "price": 100000, "brand": "yamaha"},
{"name": "guitar", "price": 100000, "brand": "kramer"}
]
}
,這是我的腳本,下面從here
update product_map t1
set data = (
select jsonb_agg(val)
from (
select case
when value->>'brand' = 'yamaha' then jsonb_set(value, '{price}', '3200')
else value end val
from product_map t2, jsonb_array_elements(data->'products')
where t1.merchant = t2.merchant
and t2.merchant like '0002%'
) s
)
where t1.merchant like '0002%';
答案
沒有錯誤,但我的json更改爲
[
{"name": "guitar", "price": 3200, "brand": "yamaha"},
{"name": "guitar", "price": 100000, "brand": "kramer"}
]
我想在這種情況下更新數據是「價格」, 但我不想更改json格式。 任何幫助將不勝感激。
謝謝
[考慮標準化數據(https://www.google.com.br/search?q=database+normalization&ie=utf-8&oe=utf-8&gws_rd=cr&ei= xduhV_r9H8SFwgT65KyIDA) –