因此,我遇到了必須將資料從一列遷移到具有不同 jsonb 架構的“克隆”自身的問題-> 我需要將 json 決議為
["keynamed": [...{"type": "type_info", "value": "value_in_here"}]]帶有 key:value 的普通物件-字典之類的{"type_info": "value_in_here" ,...}
到目前為止,我已經嘗試在子查詢 switch case 中使用子查詢和 json 函式將“type”映射到“type_info”,然后使用 jsonb_build_object(),但這需要從 wole 表中獲取資料,我需要使用資料更新它從行開始 - 有什么比做 N 個子查詢更簡單的方法嗎?
select
jsonb_object_agg(t.k, t.v):: jsonb as _json
from
(
select
jsonb_build_object(type_, _value) as _json
from
(
select
_value,
CASE _type
...
END type_
from
(
select
(datasets ->> 'type') as _type,
datasets -> 'value' as _value
from
(
select
jsonb_array_elements(
values
-> 'keynamed'
) as datasets
from
table
) s
) s
) s
) s,
jsonb_each(_json) as t(k, v);
但我不知道如何使它成為特定行并適用于簡單的更新,例如:
UPDATE table
SET table.new_field = (subquery with parsed dict in json)
任何想法/提示如何在沒有任何外部支持的情況下使用普通 PSQL 解決它?
該表的預期輸出為:
id | old_value | new_value
---------------- ------------------------------------- ------------------------------------
1 | ["keynamed": [...{"type": "type_info", "value": "value_in_here"}]] | {"type_info": "value_in_here" ,...}
uj5u.com熱心網友回復:
根據 postgres 檔案,您可以使用帶有選擇表的更新并使用連接模式更新檔案
樣本:
UPDATE accounts SET contact_first_name = first_name,
contact_last_name = last_name
FROM salesmen WHERE salesmen.id = accounts.sales_id;
如果我理解正確,下面的查詢可以幫助你。但我無法測驗,因為我沒有采樣資料,我不知道這個查詢是否有語法錯誤。
update table t
set new_value = tmp._json
from (
select
id,
jsonb_object_agg(t.k, t.v):: jsonb as _json
from
(
select
id,
jsonb_build_object(type_, _value) as _json
from
(
select
id,
_value,
CASE _type
...
END type_
from
(
select
id,
(datasets ->> 'type') as _type,
datasets -> 'value' as _value
from
(
select
id,
jsonb_array_elements(
values
-> 'keynamed'
) as datasets
from
table
) s
) s
) s
) s,
jsonb_each(_json) as t(k, v)
group by id) tmp
where tmp.id = t.id;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326881.html
標籤:PostgreSQL jsonb
