PG insert on conflict

create table tbl_test_a(a integer primary key,b text,c text);
select ctid,* from tbl_test_a;

insert into tbl_test_a (a,b,c) values (1,'1','1') on conflict do nothing;
insert into tbl_test_a (a,b,c) values (1,'1','1') on conflict (a) do nothing;
insert into tbl_test_a (a,b,c) values (1,'2','2') on conflict (a) do update set b=EXCLUDED.b,c=EXCLUDED.c;

insert into tbl_test_a (a,b,c) values (1,'3','2'),(2,'3',case when 1=2 then 0 when 2=2 then 1 else 2 end) 
on conflict (a) do update set b=EXCLUDED.b,c=EXCLUDED.c where (tbl_test_a.b !=EXCLUDED.b or tbl_test_a.c !=EXCLUDED.c);

猜你喜欢

转载自blog.csdn.net/weixin_42767321/article/details/87971600