troubleshooting Question

MySQL conditional INSERT

Avatar of chaseivey
chaseivey asked on
MySQL Server
2 Comments1 Solution547 ViewsLast Modified:
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
ishando

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros