MySQL Insert if not exists

537 views Asked by At

I'm struggling with MySQL. I want to insert some values in a table, but only if these values don't exist already.

Ex :

INSERT INTO subnets (subnet,mask,sectionId,description)
SELECT * FROM (SELECT '174399232','24','1','VlanTest') AS tmp
WHERE NOT EXISTS ( SELECT subnet FROM subnets WHERE subnet = '174399232') LIMIT 1;

The fact is I'm having the following error :

#1060 - Duplicate column name '1'

I can't use the "INSERT IGNORE INTO" because there is no unique field, and any of them can be unique. Indeed, there are several "subnet" with the same value (0) and it can't be changed, so it can't be unique. Moreover, the primary key is in auto_increment mode so it can't be used as a "key".

The MySQL version I'm running is 5.5.57.

Can you please help me ? thanks.

0

There are 0 answers