Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 07-16-2004, 12:46 PM   #1
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
Old 07-16-2004, 12:53 PM   #2
korzon
Confirmed User
 
Join Date: Jan 2004
Posts: 1,524
Instead of that stupid ass left join use "distinct".
__________________
korzon is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 12:58 PM   #3
M_M
Confirmed User
 
Join Date: May 2004
Posts: 1,167
SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE (t2.year=2004 AND t2.month=12) OR t2.campaign_id IS NULL
M_M is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 01:14 PM   #4
DrGuile
Confirmed User
 
Join Date: Jan 2002
Posts: 2,025
hmm, just a stupid question, but why do you have a month field and a year field... ??

why not a date field?
__________________
LiveBucks / Privatefeeds - Giving you money since 1999
Up to 50% Commission!
25% Webmaster Referal
Powered by Gamma
DrGuile is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 01:28 PM   #5
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally posted by DrGuile
hmm, just a stupid question, but why do you have a month field and a year field... ??

why not a date field?
I use int 10 fields usually for unix timestamps....but I made it simple for this example =)
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 01:31 PM   #6
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally posted by M_M
SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE (t2.year=2004 AND t2.month=12) OR t2.campaign_id IS NULL
Thanks!! This works, but now I have a final argument for the WHERE clause to add that's not working.

table1 also has this field:
uid int unsigned not null key

So how can I get the above, matching UID as well?

I tried this:

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

however it only returns me the rows from table1 which are NOT in table2.
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 01:32 PM   #7
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally posted by korzon
Instead of that stupid ass left join use "distinct".
Either you didn't understand my question, or you don't know MySQL very well.....
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 01:33 PM   #8
M_M
Confirmed User
 
Join Date: May 2004
Posts: 1,167
Quote:
Originally posted by Varius
Thanks!! This works, but now I have a final argument for the WHERE clause to add that's not working.

table1 also has this field:
uid int unsigned not null key

So how can I get the above, matching UID as well?

I tried this:

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

however it only returns me the rows from table1 which are NOT in table2.
SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE (t2.year=2004 AND t2.month=12 AND t1.uid=851) OR t2.campaign_id IS NULL
M_M is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 01:37 PM   #9
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally posted by M_M
SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE (t2.year=2004 AND t2.month=12 AND t1.uid=851) OR t2.campaign_id IS NULL
Tried that one too but doesn't work. That one returns ALL rows from table1, because of the OR I suspect....
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 07-16-2004, 02:00 PM   #10
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Ok I think I've almost got it.

I need a way though to specify WHERE (month !=12 AND year !=2004) .....anyone ???

ie.

month=5, year=2004 * should come up
month=12, year=2002 * should come up
month=12, year=2004 * shouldn't come up

Right now using WHERE (month !=12 AND year !=2004), none of the above return rows.

If I can get the above into a $where, the other query will work like this:

SELECT t1.campaign_id, t2.revenue FROM table1 t1 LEFT JOIN table2 t2 ON t1.campaign_id=t2.campaign_id WHERE t1.uid=851 AND ((t2.year=2004 AND t2.month=12) OR t2.campaign_id IS NULL OR $where)
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.