语法格式1:有则更新,无则插入
insert into table_1(column_1,column_2, column_3)
select column_1,
column_2,
column_3,
from table_2
on conflict (column_1)
do update set
column_2 = excluded.column_2,
column_3 = excluded.column_3
如:
insert into ua_cj_assess_result(id,all_analysis)
select id,
(lte_health_assess+nr_health_assess)/2 as all_analysis
from ua_cj_assess_result a
on conflict (id)
do update set
all_analysis = excluded.all_analysis
语法格式2:有则不操作,无则插入
insert into table_1(column_1,column_2, column_3)
select column_1,
column_2,
column_3
from table_2
on conflict (column_1)
do nothing
注意事项:
1、conflict 里的字段必须是主键
2、关联字段必须在表中字段顺序的第一个位置
3、select出来的字段名称必须和目标表中的字段名称一致