I need to change the date column of an SQL table that contains dates and amounts from, e.g. 20170101
to 20170102
. However it can be the case that the new date 20170102
already exists. This results in an duplicate key error, because the date column is part of a unique index.
My first thought was to use INSERT ON DUPLICATE KEY:
INSERT INTO table (Date, Amount)
SELECT '20170102', Amount
FROM table
WHERE Date = '20170101'
ON DUPLICATE KEY UPDATE Amount = OldAmount + NewAmount
The part Amount = OldAmount + NewAmount
cannot work obviously. But how can I solve my issue?