我希望仅在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;
如何将查询更改为仅在值不同时才更新?
答案 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;