MySQL Lists are EOL. Please join:

List:General Discussion« Previous MessageNext Message »
From:Keith Ivey Date:July 26 2004 5:04pm
Subject:Re: INSERT if record NOT EXISTS
View as plain text  
Adaikalavan Ramasamy wrote:

>This naive syntax does not work :
>IF EXISTS (SELECT myID FROM tb WHERE firstname='Jack' AND
>lastname='Doe') ELSE (INSERT INTO tb(firstname, lastname) VALUES
>('Jack', 'Doe');
>
Assuming you have the unique index on (firstname, lastname), just do

   INSERT IGNORE INTO tb (first_name, lastname) VALUES ('Jack', 'Doe');

But how are you planning to handle multiple people named Jack Doe?

-- 
Keith Ivey <keith@stripped>
Washington, DC

Thread
INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul
  • Re: INSERT if record NOT EXISTSAlec.Cawley26 Jul
  • Re: INSERT if record NOT EXISTSgerald_clark26 Jul
  • Re: INSERT if record NOT EXISTSMichael Dykman26 Jul
    • Re: INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul
      • Re: INSERT if record NOT EXISTSMarc Slemko26 Jul
  • Re: INSERT if record NOT EXISTSHarald Fuchs27 Jul
Re: INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul
  • Re: INSERT if record NOT EXISTSAlec.Cawley26 Jul
  • Re: INSERT if record NOT EXISTSKeith Ivey26 Jul
    • Re: INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul