Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to insert some records which might have duplicates. Column to overwrite/skip is and mytable.fooFlag should be 1.

I came up with something like this:

VALUES ('somemailaddress')  
ONLY IF 'somemailaddress' DOES NOT EXIST  
AND fooFlag = 1
share|improve this question
Can you post the table's definition (CREATE TABLE ...)? – ypercubeᵀᴹ Nov 5 '12 at 17:58
Do you want the ONLY IF to apply to INSERT (only if) or to IGNORE (only if) – ypercubeᵀᴹ Dec 7 '12 at 11:31

If email is UNIQUE, that's what "INSERT IGNORE" is for!

If email is UNIQUE and you want to 'update' some other fields when it is already in the table, then that's what "INSERT ... ON DUPLICATE KEY UPDATE" is for.

share|improve this answer
INSERT  INTO mytable (email)
SELECT FROM sourcetable
WHERE NOT IN (SELECT from mytable where mytable.fooflag=1)
share|improve this answer
Can you add some detail about how or why this solves the problem? – Max Vernon Nov 5 '12 at 19:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.