I\'m trying to implement a SQL query to \"UPDATE if exists else INSERT\"
My table(Allowance
) is as below:
EmployeeID int(8) PK
Year year
No need to write custom SQL for this. MySQL already has INSERT ... ON DUPLICATE KEY UPDATE
which does exactly same thing.
If you specify ON DUPLICATE KEY UPDATE
, and a row is inserted that would cause a duplicate value in a UNIQUE
index or PRIMARY KEY
, an UPDATE of the old row is performed.
Please read URL for more details.