MySQL: Insert record if not exists in table [duplicate]

I am trying to execute the following query:

INSERT INTO table_listnames (name, address, tele)
VALUES ('Rupert', 'Somewhere', '022')
WHERE NOT EXISTS (
    SELECT name FROM table_listnames WHERE name="value"
);

But this returns an error. Basically I don’t want to insert a record if the ‘name’ field of the record already exists in another record – how to check if the new name is unique?

16 Answers
16

Leave a Comment