worldnews — 2012-08-06T12:58:52-04:00 — #1
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.
r937 — 2012-08-06T16:55:51-04:00 — #2
dude, get the php to do COUNT instead, just like your suggested IF
looping is so last millenium
worldnews — 2012-08-06T17:52:06-04:00 — #3
So you are saying that a MySQL command that has an IF statement which IF would then lead to or not to an INSERT is a "so last millenium"?
I did not know that MySQL IF has become so detested?
Well, in meanwhile I have come up with a equally nice solution to reduce load on the server while getting the job done.
But I was hoping, at least I thought, that a MySQL IF then INSERT would be a nice solution.
r937 — 2012-08-06T17:54:43-04:00 — #4
i think you misunderstood
you said your php was looping over the emails
i sugggested doing a COUNT instead of looping
totally different, eh
mittineague — 2014-09-20T13:06:29-04:00 — #5
This topic is now archived. It is frozen and cannot be changed in any way.