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

时间:2019-02-28 19:01:53

标签: postgresql postgresql-10

我希望仅在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;

如何将查询更改为仅在值不同时才更新?

1 个答案:

答案 0 :(得分:1)

您可以在更新中使用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;