PostgreSQL 10:使用此upsert函数,如果列值不同,如何更新?

问题描述 投票:0回答:1

我正在寻找更新apr只有当它不同时,现在看起来它更新,无论它是不同的还是相同的:

   INSERT INTO live_mytable (id, loan_type, apr, term, oldestyear) 
   SELECT id, loan_type, apr, term, oldestyear
   FROM   imp_mytable 
   ON CONFLICT (id,loan_type,term,oldestyear) DO update
   set apr = excluded.apr;

如果值不同,如何将此查询更改为仅更新?

postgresql postgresql-10
1个回答
2
投票

您可以在更新时使用WHERE子句:

 INSERT INTO live_mytable (id, loan_type, apr, term, oldestyear) 
   SELECT id, loan_type, apr, term, oldestyear
   FROM   imp_mytable 
 ON CONFLICT (id,loan_type,term,oldestyear) 
 DO update
   set apr = excluded.apr
 WHERE 
      live_mytable.apr IS DISTINCT FROM 
      EXCLUDED.apr;
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.