UPDATE if exists else INSERT in SQL

后端 未结 4 1871
离开以前
离开以前 2020-12-07 01:51

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         


        
4条回答
  •  离开以前
    2020-12-07 02:27

    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.

提交回复
热议问题