我如何在 postgresql 中使用带有 json 填充记录的更新?

San*_*tel 3 sql postgresql json

在 Postgresql 中,如何将 JSON_POPULATE_RECORD 与更新查询一起使用?

San*_*tel 6

我们可以使用带有 JSON_POPULATE_RECORD 的 UPDATE 查询下面是示例表名作为测试的示例,它具有三列 a、b、c,数据类型字符不同。

update test set("a","b","c")=((select a,b,c from json_populate_record(NULL::test,'{"a":"first column","b":"second column","c":"third column"}')))
Run Code Online (Sandbox Code Playgroud)

您还可以在此查询中使用 UPSERT。

insert into test select * from json_populate_record(NULL::test, '{"a":"first column","b":"column","c":"column"}') ON CONFLICT ON CONSTRAINT a_PKey DO UPDATE SET("a","b","c")=((select a,b,c from json_populate_record(NULL::test,'{"a":"first column","b":"second column","c":"third column"}'))) ;
Run Code Online (Sandbox Code Playgroud)

上述查询将插入,如果主键冲突或约束冲突,则记录将被更新。