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里的字段必须为主键或者唯一索引,可以多个字段作为唯一索引,在数据库设置唯一,不然会报


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM