<?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: MySQL: counts of multiple sub-items of each parent</title>
	<atom:link href="http://www.webveteran.com/blog/index.php/web-coding/mysql-counts-of-multiple-sub-items-of-each-parent/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webveteran.com/blog/index.php/web-coding/mysql-counts-of-multiple-sub-items-of-each-parent/</link>
	<description>Programming: coldfusion, php, javascript, sql, css, scriptaculous, seo</description>
	<lastBuildDate>Wed, 30 Jun 2010 14:15:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jules</title>
		<link>http://www.webveteran.com/blog/index.php/web-coding/mysql-counts-of-multiple-sub-items-of-each-parent/comment-page-1/#comment-860</link>
		<dc:creator>Jules</dc:creator>
		<pubDate>Tue, 27 Oct 2009 22:21:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.webveteran.com/blog/?p=574#comment-860</guid>
		<description>&lt;a href=&quot;#comment-859&quot; rel=&quot;nofollow&quot;&gt;@Steve Bryant&lt;/a&gt; 
Wow that sure looks simpler. I tried it out on the same data as my version. Yours is about 4x faster. Wish I saw that a week ago!</description>
		<content:encoded><![CDATA[<p><a href="#comment-859" rel="nofollow">@Steve Bryant</a><br />
Wow that sure looks simpler. I tried it out on the same data as my version. Yours is about 4x faster. Wish I saw that a week ago!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Bryant</title>
		<link>http://www.webveteran.com/blog/index.php/web-coding/mysql-counts-of-multiple-sub-items-of-each-parent/comment-page-1/#comment-859</link>
		<dc:creator>Steve Bryant</dc:creator>
		<pubDate>Tue, 27 Oct 2009 20:20:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.webveteran.com/blog/?p=574#comment-859</guid>
		<description>You might also look at using subqueries (hopefully formatting doesn&#039;t get totally munged):

[sourcecode lang=&quot;sql&quot;]SELECT	categories.title, categories.id,
		(
			SELECT	count(*)
			FROM	products
			WHERE	products.categoryid = categories.id
		) AS productCount,
		(
			SELECT	count(*)
			FROM	images
			WHERE	images.categoryid = categories.id
		) AS imagesCount
FROM	categories[/sourcecode]

These are correlated subqueries, so if the number of categories is very large then there could be some performance implications, but I have found them to perform well generally.

I find this clearer and easier to read. The code is half is long and I think the intent is a bit more obvious (though I could be biased). It also works the same on every database I have used.</description>
		<content:encoded><![CDATA[<p>You might also look at using subqueries (hopefully formatting doesn&#8217;t get totally munged):</p>
<pre class="brush: sql;">SELECT	categories.title, categories.id,
		(
			SELECT	count(*)
			FROM	products
			WHERE	products.categoryid = categories.id
		) AS productCount,
		(
			SELECT	count(*)
			FROM	images
			WHERE	images.categoryid = categories.id
		) AS imagesCount
FROM	categories</pre>
<p>These are correlated subqueries, so if the number of categories is very large then there could be some performance implications, but I have found them to perform well generally.</p>
<p>I find this clearer and easier to read. The code is half is long and I think the intent is a bit more obvious (though I could be biased). It also works the same on every database I have used.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
