简体   繁体   中英

PostgreSQL v9.5+ UPSERT

It looks like PostgreSQL v9.5 got support for INSERT ... ON CONFLICT DO NOTHING/UPDATE http://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=168d5805e4c08bed7b95d351bf097cff7c07dd65

I want to do something like this

UPSERT INTO "myScheme"."myTable" (id, a, b, c) VALUES ($1, $2, $3, $5) WHERE id = $4'

How to achive this result taking into considiration that I am using PostgreSQL v9.5+

It could be like this:

INSERT INTO myScheme.myTable(id, a, b, c) VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO
UPDATE SET a=$2, b=$3, c=$4;

If there is no record with id=$1 then it will do insert. Otherwise it will update existing record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM