Link to home
Start Free TrialLog in
Avatar of chaseivey
chaseivey

asked on

MySQL conditional INSERT

Hello,

I am trying to do a conditional INSERT based on whether a product # already exists in my table.
Does anyone know how to do this?  I have tried several options, including the EXISTS and IF clauses, but it keeps throwing a syntax error.  Here's what I've tried so far:

INSERT INTO products ( productNum, price )
VALUES (12345, 100)
WHERE NOT EXISTS ( SELECT * FROM products WHERE productNum=12345 )

and...

IF NOT EXISTS (  SELECT * FROM products WHERE productNum=12345 )
THEN INSERT INTO products ( productNum, price )
           VALUES (12345, 100)
END IF


Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of ishando
ishando
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chaseivey
chaseivey

ASKER

Thanks man.  mySQL won't let you do a subquery on the same table, but the FROM DUAL worked like a charm.