MySQL Conditional Insert

前端 未结 13 1395
予麋鹿
予麋鹿 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:22

    You can also use INSERT IGNORE which silently ignores the insert instead of updating or inserting a row when you have a unique index on (user, item).

    The query will look like this:

    INSERT IGNORE INTO x_table(instance, user, item) VALUES (919191, 123, 456)
    

    You can add the unique index with CREATE UNIQUE INDEX user_item ON x_table (user, item).

提交回复
热议问题