postgres jsonb_set multiple keys update

后端 未结 2 1052
南笙
南笙 2020-12-05 09:21

I have DB table with jsonb column.

number  | data
    1   | {\"name\": \"firstName\", \"city\": \"toronto\", \"province\": \"ON\"}

I need a

2条回答
  •  执笔经年
    2020-12-05 09:52

    You can try this

    Here we are using jsonb concatation operator || to Concatenate two jsonb objects

    update table_name set data = (select val from (
    (select 
    CASE WHEN data ? key THEN jsonb_set(data, '{' || key || '}', quote_nullable(updated_value))
    ELSE 
    data || ('{' || quote_ident(key) || ':' || quote_ident(some_value) || '}')::jsonb
    END val
     from json_each_text((select data::json from tbl))
    CROSS JOIN tbl t
    where key in ('city','phone','prefix') and number=1)) where number=1
    

提交回复
热议问题