MySQL Forums
Forum List  »  InnoDB

Help needed with Left Join statements
Posted by: Chris Cox
Date: August 09, 2008 11:30AM

I am having a problem which may be a simple fix.

I have a table called Months, and contains only one column called mnth. It has integers 1 through 12.

I want to LEFT JOIN that table with two additional independent tables, so that I can show the count of records in the these tables for each month, and it will show zero if no records exist for any particular month. It works fine if I only join 1 additional table, but when I do it with two tables, the results are wrong.

I am using 3 tables (months, received, and sent).

Here is my example:


SELECT mnth,
COUNT(received.`timestamp`) AS received,
COUNT(sent.`timestamp`) AS sent

FROM months

LEFT JOIN received ON months.mnth= MONTH(received.`timestamp`)
AND received.user_id='1'
AND YEAR(received.`timestamp`)='2008'

LEFT JOIN sent ON months.mnth= MONTH(sent.`timestamp`)
AND sent.user_id='1'
AND YEAR(sent.`timestamp`)='2008'

GROUP BY mnth WITH ROLLUP

Options: ReplyQuote


Subject
Views
Written By
Posted
Help needed with Left Join statements
3505
August 09, 2008 11:30AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.