Avoiding inserting duplicate rows in mySQL

后端 未结 4 2095
刺人心
刺人心 2020-12-09 22:22

I have a table with an auto_inc id (primary key). I am trying to avoid the insertion of duplicate rows.

Example of a duplicate row:

id           


        
4条回答
  •  情深已故
    2020-12-09 23:07

    You can use this also.

    INSERT INTO `tableName` ( `field1`, `field2`,`field3`)
    SELECT `field1`, `field2`,`field3` FROM `tableName`
    WHERE NOT EXISTS (SELECT 1
        FROM `tableName`
        WHERE 
        `field1` = 'value' AND `field2` = 'value'  AND `field3` = 'value'
        );
    

提交回复
热议问题