Unix Technical Forum

Left join isn't joining

This is a discussion on Left join isn't joining within the MySQL forums, part of the Database Server Software category; --> Hello, I've got a select statement that joins two tables. -- tblPageHitCalendar contains a single column holding dates. It ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:13 AM
Bill
 
Posts: n/a
Default Left join isn't joining

Hello,

I've got a select statement that joins two tables.
-- tblPageHitCalendar contains a single column holding dates. It has
every day since our web site went live.
-- tblPageHits has a row for every time a page was hit.

I want this statement to tell me how many hits per day a page got in a
specified date range. It does that, but if the page got no hits on a
particular day, that day isn't returned. It should show the day and
zero, but the day isn't returned at all. What have I done wrong here?

Thanks,
Bill

SELECT c.hitdate,
COUNT(h.url)
FROM tblPageHitCalendar c
LEFT JOIN tblPageHits h ON c.hitdate = date_format(h.hittime,
'%Y-%m-%d')
WHERE c.hitdate BETWEEN '2005-04-01' AND '2005-07-31 23:59:59'
AND h.url = '/mission.php'
GROUP BY c.hitdate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:13 AM
Dikkie Dik
 
Posts: n/a
Default Re: Left join isn't joining

If you require a value from both tables in the WHERE clause, you might
as well use an inner join. The left join probably IS executed, but you
filter out any empty records with your WHERE clause.

Are you sure you don't mean a right join here?

Bill wrote:
> Hello,
>
> I've got a select statement that joins two tables.
> -- tblPageHitCalendar contains a single column holding dates. It has
> every day since our web site went live.
> -- tblPageHits has a row for every time a page was hit.
>
> I want this statement to tell me how many hits per day a page got in a
> specified date range. It does that, but if the page got no hits on a
> particular day, that day isn't returned. It should show the day and
> zero, but the day isn't returned at all. What have I done wrong here?
>
> Thanks,
> Bill
>
> SELECT c.hitdate,
> COUNT(h.url)
> FROM tblPageHitCalendar c
> LEFT JOIN tblPageHits h ON c.hitdate = date_format(h.hittime,
> '%Y-%m-%d')
> WHERE c.hitdate BETWEEN '2005-04-01' AND '2005-07-31 23:59:59'
> AND h.url = '/mission.php'
> GROUP BY c.hitdate
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:13 AM
John A. Bailo
 
Posts: n/a
Default Re: Left join isn't joining

Dikkie Dik wrote:
> If you require a value from both tables in the WHERE clause, you might
> as well use an inner join. The left join probably IS executed, but you
> filter out any empty records with your WHERE clause.
>
> Are you sure you don't mean a right join here?


Yep, he wants the outer join.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 07:13 AM
Bill
 
Posts: n/a
Default Re: Left join isn't joining

OK, that makes sense, but how would I specify the date range or page
without a WHERE clause? I tried switching the WHERE to use the same
table but it gives me the same result, probably because it's on the
right side of the join.

I also tried a right join and it returns exactly the same thing.

Thanks!
Bill

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 07:13 AM
Bill
 
Posts: n/a
Default Re: Left join isn't joining

Wait, it's the HAVING clause, isn't it? I'll have to give it a try.
It's been too long since I've played with SQL....

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 07:13 AM
Bill Karwin
 
Posts: n/a
Default Re: Left join isn't joining

"Bill" <bbois@hotmail.com> wrote in message
news:1135294076.263000.15890@g14g2000cwa.googlegro ups.com...
> Hello,
>
> I've got a select statement that joins two tables.
> -- tblPageHitCalendar contains a single column holding dates. It has
> every day since our web site went live.
> -- tblPageHits has a row for every time a page was hit.
>
> I want this statement to tell me how many hits per day a page got in a
> specified date range. It does that, but if the page got no hits on a
> particular day, that day isn't returned. It should show the day and
> zero, but the day isn't returned at all. What have I done wrong here?
>
> Thanks,
> Bill
>
> SELECT c.hitdate,
> COUNT(h.url)
> FROM tblPageHitCalendar c
> LEFT JOIN tblPageHits h ON c.hitdate = date_format(h.hittime,
> '%Y-%m-%d')
> WHERE c.hitdate BETWEEN '2005-04-01' AND '2005-07-31 23:59:59'
> AND h.url = '/mission.php'
> GROUP BY c.hitdate


It's because you put the condition h.url = '/mission.php' in the WHERE
clause, which is evaluated after the join is performed.

On days where there are no hits, NULL is the result of h.url. This is
clearly not equal to '/mission.php', so the row of the result set is
excluded and you don't get to see the day.

The better way to restrict rows in the right-hand-side of a left outer join,
put the condition inside the join condition clause.

.... FROM tblPageHitCalendar c
LEFT JOIN tblPageHits h ON (c.hitdate = date_format(h.hittime,
'%Y-%m-%d') AND h.url = '/mission.php')
WHERE c.hitdate BETWEEN '2005-04-01' AND '2005-07-31 23:59:59'

It's okay to put conditions for tblPageHitCalendar in the WHERE clause,
because it's on the left side of the join that is certain to have results.

Regards,
Bill K.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 07:13 AM
Bill
 
Posts: n/a
Default Re: Left join isn't joining

Beautiful! I get it now. Thanks for your help!

Cheers,
Bill

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 07:13 AM
Dikkie Dik
 
Posts: n/a
Default Re: Left join isn't joining

Use:

.... ((c.hitdate BETWEEN '2005-04-01' AND '2005-07-31 23:59:59') OR
c.hitdate IS NULL)

if you want both the records with the hitdates in the range and the
results for which no c.hitdate exists. Just try your query in steps and
you see what happens. Try it without a WHERE clause, with a restriction
on the c table and with restrictions on both tables.

Bill wrote:
> OK, that makes sense, but how would I specify the date range or page
> without a WHERE clause? I tried switching the WHERE to use the same
> table but it gives me the same result, probably because it's on the
> right side of the join.
>
> I also tried a right join and it returns exactly the same thing.
>
> Thanks!
> Bill
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:17 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com