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?
I’m not actually suggesting that you do this, as the
UNIQUEindex as suggested by Piskvor and others is a far better way to do it, but you can actually do what you were attempting:Insert a record:
Try to insert the same record again:
Insert a different record:
And so on…
Update:
To prevent
#1060 - Duplicate column nameerror in case two values may equal, you must name the columns of the inner SELECT: