how to use if not exists while inserting a row mysql

后端 未结 3 2052
我寻月下人不归
我寻月下人不归 2021-01-24 08:17

The thing that i want to do is to check if there is a same row and if not to insert a new row to the table.

In that case, how can i use if not exists ?

相关标签:
3条回答
  • 2021-01-24 08:58
    INSERT INTO `facebook` (`ID`,`fb_id`,`label_id`,`page_ids`,`token`) 
    SELECT NULL, '". $session['id'] ."', '$id', '', '". $access_token ."'
    FROM DUAL
    WHERE NOT EXISTS (SELECT 'x' FROM facebook WHERE label_id = '$id')
    

    I think FROM DUAL is optional, but I'm an Oracle guy. :)

    0 讨论(0)
  • 2021-01-24 09:14

    User Insert Into to ignore duplicates:

    INSERT IGNORE INTO `facebook`
    SET `ID` = null,
    `fb_id` = '". $session['id'] ."',
    `label_id` = '$id',
    `page_ids`='',
    `token`='". $access_token ."';
    
    0 讨论(0)
  • 2021-01-24 09:20

    You can use the INSERT IGNORE INTO ... syntax. When using this, duplicate key errors are treated as warnings and the statement will return without having inserted the affected row.

    0 讨论(0)
提交回复
热议问题