问题
I'm looking to update apr
only if it's different, right now, it looks like it updates regardless if it's different or same:
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;
How can this query be changed to only update if value is different?
回答1:
You can use a WHERE
clause on the update:
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;
来源:https://stackoverflow.com/questions/54932543/postgresql-10-with-this-upsert-function-how-to-update-only-if-column-value-is