0

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.

Ben Gordon
  • 437
  • 3
  • 10
  • 23

1 Answers1

1

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
Community
  • 1
  • 1
Enpi
  • 281
  • 1
  • 9