Jump to content


Photo

not included in join


  • Please log in to reply
1 reply to this topic

#1 daiv

daiv
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 21 March 2006 - 11:19 PM

Hi,

I have a join between 3 tables that finds users that are active, their email address and their backup log for the previous day. I then send them an email with the information. this works fine with the code below

SELECT EmailNotify . * , FTPUsers.Firstname, FTPUsers.LastName, UserTable . *
FROM EmailNotify
LEFT JOIN FTPUsers ON EmailNotify.Username = FTPUsers.Username
LEFT JOIN UserTable ON EmailNotify.UserName = UserTable.UserID
WHERE EmailNotify.active =1
AND UserTable.Date = DATE_SUB( CURDATE( ) , INTERVAL 1
DAY )
LIMIT 0 , 30

What I need to find now is everyone who DID NOT do a backup the previous day and send an email based on that. FTPUsers has the users first and last names and other account info, UserTable includes the backup information including UserID, Date, RecordID among other information.

I am looking for a join that will give me a list of people who want email notifications everyday about the backups, but did not actually do one the previous day so I can send an email saying "no backup perfromed on this date"

.....

As I wrote that up their I figured out the answer. I do a similar join but instead of checking the date I use a NOT IN clause and give a list of the names returned from the first query, and it returns the records that were not included previously due to lack of backup activity. While looping through the first set of results I ad the UserID's to a list then can pass that into the second query.

Maybe this will help someone else. and example of the query I'd pass would look like this.

SELECT EmailNotify.* , FTPUsers.Firstname, FTPUsers.LastName FROM EmailNotify LEFT JOIN FTPUsers ON EmailNotify.Username = FTPUsers.Username 
WHERE EmailNotify.active =1
AND EmailNotify.UserName NOT
IN (
'David'
)
LIMIT 0 , 30


Does anyone else have another suggestion for a query that does not require the generated list?


Thanks,
Daiv

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 March 2006 - 08:10 AM

You could always use a LEFT JOIN ... (matching conditions) WHERE ... IS NULL, which will, by definition, return the non-matching rows.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users