MySQL Conditional Insert

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

    In case you don't want to set a unique constraint, this works like a charm :

    INSERT INTO `table` (`column1`, `column2`) SELECT 'value1', 'value2' FROM `table` WHERE `column1` = 'value1' AND `column2` = 'value2' HAVING COUNT(`column1`) = 0
    

    Hope it helps !

提交回复
热议问题