PostgreSQL upsert:如果字段没有改变,什么都不做

时间:2017-01-05 10:20:41

标签: sql postgresql upsert

是否可以表达一个upsert查询,如果插入的数据与数据库中的数据相比没有任何变化,那么什么都不会发生?

目前我有:

insert into feeds (link, title, category)
values (...)
on conflict (link)
do update set (title, category, _updated)
= (..., now()::timestamp)

1 个答案:

答案 0 :(得分:9)

您可以在where部分添加update子句:

insert into feeds (link, title, category)
values (...)
on conflict (link)
do update 
   set (title, category, _updated) = (..., now()::timestamp)
where (feeds.title, feeds.category) is distinct from (excluded.title, excluded.category)