jsonb_set()函数更新数据

最近更新时间: 2025-02-18 16:02:00

  • 复制
    复制成功
jsonb_set(target jsonb, path text[], new_value jsonb, [create_missing boolean]) 

说明:

target指要更新的数据源,path指路径,new_value指更新后的键值,create_missing值为true表示如果键不存在则添加,create_missing值为false表示如果键不存在则不添加。

  • 复制
    复制成功
postgres=# update t_jsonb set f_jsonb = jsonb_set( f_jsonb , '{col}' , '"pgxz"' , true ) where id=1;
UPDATE 1
postgres=# update t_jsonb set f_jsonb = jsonb_set( f_jsonb , '{col}' , '"pgxz"' , false ) where id=2;         
UPDATE 1
postgres=# update t_jsonb set f_jsonb = jsonb_set( f_jsonb , '{col2}' , '"pgxz"' , false ) where id=3;          
UPDATE 1
postgres=# select * from t_jsonb;
 id |                   f_jsonb                   
----+---------------------------------------------
  1 | {"col": "pgxz", "col1": 1, "col2": "TDSQL PG"}
  2 | {"col1": 1, "col2": "TDSQL PG"}
  3 | {"col1": 1, "col2": "pgxz"}
(3 rows)