0

Im trying to doing a sql query that is not working, I would like to add one element to a table only and only if that value is not in the table, something like that:

IF EXISTS (Select `id` from `artists` Where `artist`='"$artist"') Do Nothing
ELSE      Insert into `artists` (`artist`) values('".$artist."')

What is the best way to do that?

PS: I have tried to look for that information by google and in the forum, but I dont understand the idea

Thank you very much in advance

PS2: Sorry, Its a MYSQL database

Taryn
  • 242,637
  • 56
  • 362
  • 405

1 Answers1

2

What about using IF NOT EXISTS...

Something like:

IF NOT EXISTS (Select `id` from `artists` Where `artist`='"$artist"') Insert into `artists` (`artist`) values('".$artist."')
Derek
  • 7,615
  • 5
  • 33
  • 58