Jump to content

Specific Join Query Problem


gordonfreeman83

Recommended Posts

Hi all, I would appreciate any help at all.

 

I'm trying to do a specific query on two tables, and am not gettin the results im expecting.

 

We have two tables, Accounts (client information, ID etc) and Notes (notes against accounts with matching ID ). I want to find all the accounts who haven't had a note made against them for the last 6 months.

 

After reading the fine manual i thought this might do it:

 

select accounts.name, accounts.phone_office from accounts left join notes on accounts.id = notes.id where notes.id is NULL and notes.date_created between '2006-10-15' and '2007-03-08';

 

Using the notes.id is NULL works as the manual says, finding all the accounts.id that have no corresponding notes.id and thus no notes against them, but it comes back with no result when combined with the note date where clause, even though there are many match

 

Have i made a mistake in my logic here, or got the query completely wrong? Thanks in advance for any help!

 

Peter

 

Link to comment
Share on other sites

You have conflicting WHERE clauses... note date CANT be between that range when it doesn't exist. Also, note.id is not going to be the FK relating to account.id

SELECT a.name, a.phone_office 
FROM accounts AS a
LEFT JOIN notes AS n ON  as.id = n.account_id 
     AND n.date_created > DATE_SUB(NOW(),INTERVAL 6 MONTH)
WHERE n.id IS NULL;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.