<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Tate-Davies &#187; group_concat</title>
	<atom:link href="http://www.christatedavies.co.uk/tag/group_concat/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christatedavies.co.uk</link>
	<description>An archive of helpful tit bits of information for development, and probably some stuff that is incomplete, wrong or boring...</description>
	<lastBuildDate>Tue, 31 Jan 2012 11:18:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL Group Concat</title>
		<link>http://www.christatedavies.co.uk/2010/03/18/mysql-group-concat/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/18/mysql-group-concat/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 00:29:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[group_concat]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=11</guid>
		<description><![CDATA[What is MySQL&#8217;s GROUP_CONCAT function for? How often have you wanted a comma separated list of values in a table of results. Say you had a list of trainers in makes, and sizes? And you want to display this list as a grid: Make &#124; Size ---------------------- Adidas &#124; 6 Adidas &#124; 7 Adidas &#124; [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/mysql-group-concat/' addthis:title='MySQL Group Concat ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<h2>What is MySQL&#8217;s GROUP_CONCAT function for?</h2>
<p>How often have you wanted a comma separated list of values in a table of results. Say you had a list of trainers in makes, and sizes? And you want to display this list as a grid:</p>
<pre>Make   | Size
----------------------
Adidas | 6
Adidas | 7
Adidas | 8
Adidas | 10
Nike   | 7
Nike   | 8
Puma   | 5
Puma   | 7
Puma   | 10
Puma   | 12</pre>
<p>Now, if these records are in one table, how are you going to get the sizes for each trainer?</p>
<p>You could loop through each group of trainers, and output a string, size by size. Or, you could use the GROUP_CONCAT function in MySQL, which will give you a list of the sizes as one field, even though they are multiple records.</p>
<pre>SELECT make,
GROUP_CONCAT(DISTINCT size ORDER BY size ASC SEPARATOR ", " ) AS trainer_size
FROM trainers GROUP BY make ASC;</pre>
<p>Then all you need is to loop through the 3 records, of each will contain 2 fields: make and trainer_size. Its a lot easier than writing code to loop through the DISTINCT makes and get the available sizes.</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/mysql-group-concat/' addthis:title='MySQL Group Concat ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/18/mysql-group-concat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

