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

Mic*_*ent 6 sql postgresql upsert

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

目前我有:

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

a_h*_*ame 11

您可以whereupdate零件中添加一个子句:

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)
Run Code Online (Sandbox Code Playgroud)

  • 我的查询底部有一个返回语句:返回 feed_uuid。当我插入一条新记录或其中一个字段发生变化时,它会返回一个 id。但是当没有插入/更新时,它什么都不返回。什么是解决这个问题的方法? (4认同)