<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Nested LEFT JOINs to link three or more tables</title>
	<atom:link href="http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/feed" rel="self" type="application/rss+xml" />
	<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables</link>
	<description>The life and thoughts of Ryan McElroy</description>
	<pubDate>Thu, 08 Jan 2009 00:44:05 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ryan</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-151665</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Tue, 14 Oct 2008 21:29:08 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-151665</guid>
		<description>Yes, that's where I did this query.</description>
		<content:encoded><![CDATA[<p>Yes, that&#8217;s where I did this query.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rufor</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-151425</link>
		<dc:creator>Rufor</dc:creator>
		<pubDate>Sun, 12 Oct 2008 16:02:38 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-151425</guid>
		<description>can i use it in MySQL queries?</description>
		<content:encoded><![CDATA[<p>can i use it in MySQL queries?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Snook</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-1043</link>
		<dc:creator>Jonathan Snook</dc:creator>
		<pubDate>Mon, 22 Aug 2005 19:33:38 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-1043</guid>
		<description>You shouldn't have had to nest them to do what you wanted. The following should do the same:

&lt;code&gt;SELECT `accounts`.`name` AS account_name,
`contacts`.`name` AS contact_name,
`date`, `amount`, `type`, `detail`, `memo`, `cleared`
FROM `transactions` 
LEFT JOIN `accounts` ON `accounts`.`id` = `account_id`
LEFT JOIN `contacts` ON `contacts`.`id` = `contact_id`
WHERE `parent_id` = 0&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>You shouldn&#8217;t have had to nest them to do what you wanted. The following should do the same:</p>
<p><code>SELECT `accounts`.`name` AS account_name,<br />
`contacts`.`name` AS contact_name,<br />
`date`, `amount`, `type`, `detail`, `memo`, `cleared`<br />
FROM `transactions`<br />
LEFT JOIN `accounts` ON `accounts`.`id` = `account_id`<br />
LEFT JOIN `contacts` ON `contacts`.`id` = `contact_id`<br />
WHERE `parent_id` = 0</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bernie Zimmermann</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-1042</link>
		<dc:creator>Bernie Zimmermann</dc:creator>
		<pubDate>Mon, 22 Aug 2005 16:25:28 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-1042</guid>
		<description>It's funny how these things just seem to pop up at the same time...

&lt;a href="http://www.snook.ca/archives/000399.php" title="Database Joins"&gt;Database Joins&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>It&#8217;s funny how these things just seem to pop up at the same time&#8230;</p>
<p><a href="http://www.snook.ca/archives/000399.php" title="Database Joins">Database Joins</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bernie Zimmermann</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-1038</link>
		<dc:creator>Bernie Zimmermann</dc:creator>
		<pubDate>Mon, 22 Aug 2005 04:14:37 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-1038</guid>
		<description>I understand now.  I was letting the actual data being queried get in the way of the problem.  Thanks for clarifying.</description>
		<content:encoded><![CDATA[<p>I understand now.  I was letting the actual data being queried get in the way of the problem.  Thanks for clarifying.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-1032</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Sun, 21 Aug 2005 14:58:56 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-1032</guid>
		<description>Bernie, I think you misunderstand the information I am going after. Here is the situation: I have a long list of transactions, which may or may not have associated contact people and account information. I want the transactions to display even if the contact person is not defined, and even if the account is not defined. In other words, I want to display all of the transactions, no matter what. But if aditional information happensto be associated with a transaction, I'd like that information as well. In a two-table case, this is a normal straightforward LEFT JOIN. The result is that the table on the left returns all of its rows, even if there is no matching entry in the table on the right. Let me give you an example of where this is neccesary:

Lets say you have a database of students at a school. You have all of the student names in one table, 'students'. Then you have a table called 'absences.' In that table, you store a date and a student_id - this information can be combined to return how many absences each student has:

&lt;code&gt;SELECT name, absences FROM students, absences WHERE student_id = students.id GROUP BY students.id;&lt;/code&gt;

This would return a list of all students &lt;em&gt;that have absences&lt;/em&gt;. But what if I wanted a list of &lt;em&gt;all students&lt;/em&gt; including those that have no absences? Then I use a LEFT JOIN:

&lt;code&gt;SELECT name, absences FROM students LEFT JOIN absences ON student_id = students.id GROUP BY students.id;&lt;/code&gt;

This nifty query returns a row for every student, even if there are no matching records in the absences table. Clearly, LEFT JOIN serves a useful purpose.

However, I wanted to take it a step further. In my table, I have transactions that may or may not have an associated account, and that may or may not have an associated contact. I want all the transactions, even if the account is not defined, and even if the contact is not defined. But in the case that the account or the contact is defined, I want that information too. So the &lt;em&gt;nested&lt;/em&gt; LEFT JOIN makes this possible.

I hope that clears things up.</description>
		<content:encoded><![CDATA[<p>Bernie, I think you misunderstand the information I am going after. Here is the situation: I have a long list of transactions, which may or may not have associated contact people and account information. I want the transactions to display even if the contact person is not defined, and even if the account is not defined. In other words, I want to display all of the transactions, no matter what. But if aditional information happensto be associated with a transaction, I&#8217;d like that information as well. In a two-table case, this is a normal straightforward LEFT JOIN. The result is that the table on the left returns all of its rows, even if there is no matching entry in the table on the right. Let me give you an example of where this is neccesary:</p>
<p>Lets say you have a database of students at a school. You have all of the student names in one table, &#8217;students&#8217;. Then you have a table called &#8216;absences.&#8217; In that table, you store a date and a student_id - this information can be combined to return how many absences each student has:</p>
<p><code>SELECT name, absences FROM students, absences WHERE student_id = students.id GROUP BY students.id;</code></p>
<p>This would return a list of all students <em>that have absences</em>. But what if I wanted a list of <em>all students</em> including those that have no absences? Then I use a LEFT JOIN:</p>
<p><code>SELECT name, absences FROM students LEFT JOIN absences ON student_id = students.id GROUP BY students.id;</code></p>
<p>This nifty query returns a row for every student, even if there are no matching records in the absences table. Clearly, LEFT JOIN serves a useful purpose.</p>
<p>However, I wanted to take it a step further. In my table, I have transactions that may or may not have an associated account, and that may or may not have an associated contact. I want all the transactions, even if the account is not defined, and even if the contact is not defined. But in the case that the account or the contact is defined, I want that information too. So the <em>nested</em> LEFT JOIN makes this possible.</p>
<p>I hope that clears things up.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bernie Zimmermann</title>
		<link>http://arcanius.silverfir.net/blog/nested-left-joins-to-link-three-or-more-tables/comment-page-1#comment-1028</link>
		<dc:creator>Bernie Zimmermann</dc:creator>
		<pubDate>Sat, 20 Aug 2005 01:49:18 +0000</pubDate>
		<guid isPermaLink="false">/?p=375#comment-1028</guid>
		<description>I'm unclear on why you would be interested in the transaction information along with the accounts and contacts information but not care if they match up.  The only reason I can think of to include the accounts and contacts information is if it matches up with the rows you're fetching from transactions.

If you don't care about accounts and contacts information, simply leave those two tables out of the query.

There's gotta be an easier way to do what you're doing, but I can't quite figure out the reasoning behind what you're doing in the first place ;)

I've had to query some pretty crazy data before and have never had to do the kind of thing you're demonstrating here.</description>
		<content:encoded><![CDATA[<p>I&#8217;m unclear on why you would be interested in the transaction information along with the accounts and contacts information but not care if they match up.  The only reason I can think of to include the accounts and contacts information is if it matches up with the rows you&#8217;re fetching from transactions.</p>
<p>If you don&#8217;t care about accounts and contacts information, simply leave those two tables out of the query.</p>
<p>There&#8217;s gotta be an easier way to do what you&#8217;re doing, but I can&#8217;t quite figure out the reasoning behind what you&#8217;re doing in the first place ;)</p>
<p>I&#8217;ve had to query some pretty crazy data before and have never had to do the kind of thing you&#8217;re demonstrating here.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
