PgSQL upsert批量查询插入或更新(insert select/on conflict do update踩坑记录)

insert into t --进行插入
values(1,'name') 
ON CONFLICT(id) --如果id这个键存在
do update set --更新以下字段
name=EXCLUDED.name ;
 
 
insert into t (a1,b1,c1)
select a2,b2,c2
from t2 
on conflict(a1) 
do update set 
(b1,c1) = (1,2)

conflict里的字段必须为主键或者唯一索引,可以多个字段作为唯一索引,在数据库设置唯一,不然会报

posted @ 2021-09-07 11:25  一心二念  阅读(3709)  评论(0编辑  收藏  举报