Insert email if it doesn't exist in the sent table
I have two mysql tables sent
and queue
. I insert members into the queue
table. However I don't want to insert that same memb开发者_运维问答er again--if they're in the sent
table. They were already sent an invitation.
How can I check if the member inserted into queue
doesn't exist in sent
?
if (insertEmail($email)) {
echo "<span class='success'>Successfully added " . $email . " to the queue</span><br/>";
} else {
echo "<span class='fail'>Error we already sent " . $email . " an invitation email.</span><br/>";
}
function insertEmail($email)
{
mysql_query("INSERT INTO queue (email) SELECT `$email` FROM dual WHERE `$email` NOT IN (SELECT `email` FROM `sent`)");
}
Quassnoi helped me get the query but I think I'm doing something wrong. When I try to insert emails into the queue I always get Unknown column '[removed]@gmail.com' in 'field list'
Perhaps you don't have the ability to change your tables around, but if you do, have you considered creating a status column on the queue table and removing the sent table entirely?
The simplest possible version of this would be:
Table 'queue':
email status
me@email.com queued
someone@email.com queued
another@email.com sent
Then whenever your queue processor runs, the insertEmail becomes updateQueue($email) and your query is:
mysql_query('Update queue Set `status' = 'sent' Where email = '$email');
To retrieve anybody that has not yet been sent an email:
mysql_query('Select email From queue Where `status' = 'queued');
精彩评论