TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-05-2008, 07:09 AM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Inner Join vs Left Join

Just curious if I have this correct?

An SQL inner join will return all records, but only if they match the conditions defined by the ON clause, and a left join will return all records, even if the conditions in the ON clause aren't met? Boiled down?
-m
delayedinsanity is offline  
Reply With Quote
Old 08-05-2008, 03:26 PM   #2 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

not quite, If i remember correctly the left join returns all records when the first table (or the left one) has a value that is not null and still meets the conditions of the ON clause. And the Inner join returns when both of them are not null. Outer returns all rows as long as one of them is not null and right does the opposite of left.

Pretty sure thats how it works.
CoryMathews is offline  
Reply With Quote
Old 08-05-2008, 05:51 PM   #3 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Here is a pretty nice illustration of the join types. (cool blog too...)

It's one of those "hard to explain with words" things I think. It is easier for me to visualize it.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 08-05-2008, 06:12 PM   #4 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 295
Thanks: 8
tony is on a distinguished road
Default

I read that blog, but I've never seen that. it's easy with illustrations for me.
tony is offline  
Reply With Quote
Old 08-05-2008, 07:16 PM   #5 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

I have read that blog for a little while now. I may not always agree with some of his conclusions, but it is at least interesting and intelligent.

I'm kind of anxious to see what his new site is going to be like. I HATE experts exchange.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll

Last edited by buggabill : 08-05-2008 at 07:22 PM. Reason: grrr...spelling :-)
buggabill is offline  
Reply With Quote
Old 08-05-2008, 08:22 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I might've said that a little wrong, but okies, I think I get it. I was a little confused at first because I'm just learning about joins - I have to at the moment, because while I'm relatively familiar with the idea of using multiple tables to present information in the most optimal format (such as using a primary table to hold data, with corresponding column ids to pull information out of other tables, such as related categories etc), I'm not so up to speed on how to connect all that data inside of a single query.

My first simple query was to pull out a news item from the main table, and then get the appropriate category name based on it's ID from another table. So I did this:

Code:
SELECT news.id, news_categories.name AS category, news.uid, news.headline, news.body
FROM `news`
INNER JOIN `news_categories` ON news.cid = news_categories.cid
WHERE news.id = '%d'
LIMIT 1
Worked fine for me, first try. Then I moved on to optimizing my next query, which already used a LEFT JOIN (credits to Orc for helping me on that one) and I thought wait a minute... so I went back to the first, and switched the INNER with a LEFT and got the exact same result. At that point I wasn't clear what I had just done... so I went on to the next query to see if it produced the same results;

Code:
SELECT news.id, news.uid, news_categories.name AS category, news.headline, news.body, count( DISTINCT news_comments.id ) AS total_comments
FROM `news`
LEFT JOIN `news_categories` ON news.cid = news_categories.cid
LEFT JOIN `news_comments` ON news_comments.nid = news.id
WHERE news.cid = '%d'
GROUP BY news.id
ORDER BY news.id DESC
...and I switched the LEFT with an INNER. At first I thought I had gotten the same result set back, and was starting to think that there was something suspicious going on here. Then I took a closer looked and realized the second query, when done with an INNER only returned results from `news` when there was associated comments in `news_comments`.

So that's why I boiled it down to what I did. LEFT appeared to be pulling out all results from the primary table, whether or not it could find matching results in the second table (returning NULL where there were none). INNER seemed to be only returning results from the first table that could find corresponding results in the second.

So that brings up a small second question - is a LEFT JOIN and LEFT OUTER JOIN the exact same thing? If so, are both fully acceptable by general SQL syntax, or is the shortened version something that only MySQL allows?
-m
delayedinsanity is offline  
Reply With Quote
Old 08-05-2008, 09:39 PM   #7 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

With MySQL, the "OUTER" keyword is optional. It's provided for ODBC compatibility and doesn't add any extra capabilities.
MySQL JOIN reference
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 08-05-2008, 09:39 PM   #8 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

All joins and implicitly OUTER.

Thanks for the link bugabill. I think every subject in association with images becomes a little more clear than plain text alone.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 08-05-2008, 11:48 PM   #9 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Quote:
With MySQL, the "OUTER" keyword is optional. It's provided for ODBC compatibility and doesn't add any extra capabilities.
Okay, so yes, if I want to make my queries compatible with other SQL databases, I want to include the OUTER keyword, correcto? Eventually I want to build, or incorporate an abstraction layer, and I want to make sure it migrates with as little fuss as possible. I'd get it done now, but my to do list is about a mile long and lost somewhere in my bedroom. Which isn't a mile long. Nonetheless, it's lost.
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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


All times are GMT. The time now is 08:00 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design