So, I'm trying to insert values into a table if those values aren't already present. I know this works - see https://stackoverflow.com/a/53629298/1525594
INSERT INTO table_name (name, address, tele)
SELECT * FROM (SELECT 'Nazir', 'Kolkata', '033') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_name WHERE name = 'Nazir'
) LIMIT 1;
But what I'm wondering is: can I use VALUES(name)
function so I don't have to specify the name
twice? When I try it, I get Incorrect syntax near the keyword 'VALUES'
.
INSERT INTO table_name (name, address, tele)
SELECT * FROM (SELECT 'Nazir', 'Kolkata', '033') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_name WHERE name = VALUES(name)
) LIMIT 1;
Why does this error, and is this possible, or no?
I was hoping this was possible because (different question) I would like to do a multi-row insert - perhaps using UNION to select multiple rows?
(Edit): Not a duplicate - please read the question. This question asks about inserting string values into a table without selecting them from another table, and it asks very specifically how to reference those insertion values inside the WHERE NOT EXISTS
statement.