I have a SQL syntax error with my IF NOT EXISTS
on line 1 when I tried to do this request on MySQL, and I can't figure why.
IF NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn')
BEGIN
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
END;
I'm trying to insert first_name only if there is no other same first name in my_table.
I also tried this syntax, but I still have the error 1064 "You have an error in your SQL syntax" :
IF NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn') THEN
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
END IF;
I tried SELECT * FROM
my_tableWHERE first_name = 'Testfn'
separately, and it works.
And like this doesn't work too :
INSERT INTO `my_table` (first_name)
VALUES ('Testfn')
WHERE NOT EXISTS (SELECT * FROM `my_table` WHERE first_name = 'Testfn');
EDIT : first_name is UNIQUE in the database.