我有一个jsonb列,其字段为“ tags”:{“ choices”:{choice}},并且我想将“ choices”移至同一jsonb列中的另一个字段“ questions”:{}。
请让我知道如何做到这一点。
答案 0 :(得分:0)
您可以使用PostgreSQL(9.5 / 9.6 +)JSONB函数(https://www.postgresql.org/docs/9.6/functions-json.html)jsonb_set或jsonb_insert,或将两者结合使用,例如与jsonb_set组合
postgres=# SELECT myjsonb old_value, jsonb_set(myjsonb,'{tags}',
text('{"choices":"{}","questions":'||text(myjsonb->'tags'-
>'choices')||'}')::jsonb) as new_value
postgres-#
postgres-# FROM (VALUES ('{"tags":{"choices":"{choice1}","questions":"{}"
}}'::jsonb), ('{"tags":{"choices":"{choice2}","questions":"{}" }}'::jsonb)) AS
t (myjsonb);
old_value | new_value
-------------------------------------------------------+-------------------------------------------------------
{"tags": {"choices": "{choice1}", "questions": "{}"}} | {"tags": {"choices": "{}", "questions": "{choice1}"}}
{"tags": {"choices": "{choice2}", "questions": "{}"}} | {"tags": {"choices": "{}", "questions": "{choice2}"}}
(2 filas)