View Single Post
Old 07-16-2004, 12:46 PM  
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
MySQL experts: question

Is this possible? (LEFT JOIN question)
I have two tables, I'll simplify them for the sake of this example:

table1:
campaign_id int unsigned not null default 0 key

table2:
month tinyint not null default 0 key
year smallint not null default 0 key
campaign_id int unsigned not null default 0 key
revenue double(5,2) not null default 0.00

What I"m trying to achieve is to get all the campaign_ids from table1, regardless of if they have a row in table2 or not.

This part I'm getting with:

"SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE (t2.campaign_id IS NULL OR t2.campaign_id IS NOT NULL)";

This will return me all campaign_ids from table1.

Here is my trouble:

If I want to add in my WHERE, a clause about the month/year, I always get back 0 results.

ie.

"SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE (t2.campaign_id IS NULL OR t2.campaign_id IS NOT NULL) AND t2.year=2004 AND t2.month=12";

* Is it possible for me to do this in one query? The results I want are simple, I want each campaign_id in table1 returned, with the revenue they earned IN THAT date range (if any).

Thx in advance !!
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote