Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use update with json populate record in postgresql?

In Postgresql how can I use JSON_POPULATE_RECORD with an Update query?

like image 843
Sanket Patel Avatar asked Nov 01 '25 09:11

Sanket Patel


1 Answers

We can use UPDATE query with JSON_POPULATE_RECORD Below is the example for sample table name as a test and having three columns a,b,c with data type character varying.

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"}')))

You can also use UPSERT with this query.

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"}'))) ;

The above query will insert and if primary key conflicts or constraint conflicts then Record will be Updated.

like image 124
Sanket Patel Avatar answered Nov 04 '25 00:11

Sanket Patel