MySQL Conditional Insert

前端 未结 13 1549
予麋鹿
予麋鹿 2020-11-22 09:51

I am having a difficult time forming a conditional INSERT

I have x_table with columns (instance, user, item) where instance ID is unique. I want to insert a new row

13条回答
  •  面向向阳花
    2020-11-22 10:37

    With a UNIQUE(user, item), do:

    Insert into x_table (instance, user, item) values (919191, 123, 456) 
      ON DUPLICATE KEY UPDATE user=123
    

    the user=123 bit is a "no-op" to match the syntax of the ON DUPLICATE clause without actually doing anything when there are duplicates.

提交回复
热议问题