How to make an Insert IF a condition is met
How does one write an IF statement in MySQL which IF will make an INSERT if certain condition is met.
For example say we have a Table of users with fields email & q_answered = 'no' or 'yes'
So we want to make an INSERT into this TABLE only if this user has NOT been sent a Notice over the last 3 days.
The IF INSERT then would look logically like:
IF (COUNT(id) FROM qa_history WHERE date_added > (DATE_SUB(CURDATE(), INTERVAL 3 DAY)) AND email = '$email' AND q_answered = 'no' < 3) THEN INSERT INTO qa_email_history (email = '$email' )
FYI, we currently get the above job done by having a Php code that loops through all the emails in the list to make sure the above condition is met before a new INSERT is made, but this is killing the server. So much faster would be to make the IF & INSERT into one statement.