What is the most efficient mysql query to SELECT from a table IF a value already exists ELSE INSERT?
I have already tried several options but can't find something that works for me.
What is the most efficient mysql query to SELECT from a table IF a value already exists ELSE INSERT?
I have already tried several options but can't find something that works for me.
I'm new to Stackoverflow and dont know how to tag a question as duplicate.
But i think there is a really similar question with plenty of answers. Give a look here
[Possible solution]
You could insert if it not exists and then select it.
INSERT INTO Test(Key_Value , Name , ...) VALUES(@Key_Value ,@Name,..) WHERE NOT EXISTS (SELECT 1 FROM Test WHERE KeyValue = @KeyValue);
SELECT Name FROM Test WHERE Key_Value = @Key_Value