<?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 statements &#8211; order by and group by</title>
	<atom:link href="http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/</link>
	<description></description>
	<lastBuildDate>Tue, 10 Jan 2012 01:21:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Jennifer</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8559</link>
		<dc:creator>Jennifer</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8559</guid>
		<description>Just a note to myself here - found this:
&lt;h3&gt;SELECT seminar_name 
FROM student_table GROUP BY seminar_name 

SELECT DISTINCT seminar_name 
FROM student_table&lt;/h3&gt;

on &lt;a href=&quot;http://www.phpbuilder.com/mail/php-general/2001092/1682.php&quot; target=&quot;_blank&quot;&gt;this&lt;/a&gt; page on phpbuilder. (I had better luck with that &quot;DISTINCT&quot; version)

</description>
		<content:encoded><![CDATA[<p>Just a note to myself here &#8211; found this:</p>
<h3>SELECT seminar_name<br />
FROM student_table GROUP BY seminar_name </p>
<p>SELECT DISTINCT seminar_name<br />
FROM student_table</h3>
<p>on <a href="http://www.phpbuilder.com/mail/php-general/2001092/1682.php" target="_blank">this</a> page on phpbuilder. (I had better luck with that &#034;DISTINCT&#034; version)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Gallaher</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8558</link>
		<dc:creator>Steven Gallaher</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8558</guid>
		<description>Yes, that is exactly correct.  (Sorry I took so long to come back.)</description>
		<content:encoded><![CDATA[<p>Yes, that is exactly correct.  (Sorry I took so long to come back.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jennifer</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8557</link>
		<dc:creator>Jennifer</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8557</guid>
		<description>Ok, ORDER BY - I get 100%. That&#039;s easy. It&#039;s just sorting the data.

I&#039;m *beginning* to get GROUP BY - but I don&#039;t see how your two examples are different...

If you had just:
SELECT date title FROM db
Each time you get a row (mysql_fetch_array) you can get the date and the title. (I&#039;m assuming here it would be possible to have dates duplicated... more than one title per date)

In your example:
SELECT date, count(title) as count FROM db GROUP BY date
then each time you get a row, you can display the date and the count (number of titles for that date)

like:
11/20/2002 - count:2
11/21/2002 - count: 3

And here is where (I think) I get lost, 
in your last example:
SELECT date, title, count(title) as count FROM db GROUP BY date
then each time you get a row you can display the date, and the count (number of titles for that date) AND(?) the title... so again, we&#039;re back to showing mulitple dates? so displaying the results could look like:

11/20/2002 - This is a title - count: 2
11/20/2002 - This is another title - count: 2

11/21/2002 - This is a new title - count: 3
11/21/2002 - This is yet a new title - count: 3
11/21/2002 - And our last title - count: 3

Is that correct?</description>
		<content:encoded><![CDATA[<p>Ok, ORDER BY &#8211; I get 100%. That&#039;s easy. It&#039;s just sorting the data.</p>
<p>I&#039;m *beginning* to get GROUP BY &#8211; but I don&#039;t see how your two examples are different&#8230;</p>
<p>If you had just:<br />
SELECT date title FROM db<br />
Each time you get a row (mysql_fetch_array) you can get the date and the title. (I&#039;m assuming here it would be possible to have dates duplicated&#8230; more than one title per date)</p>
<p>In your example:<br />
SELECT date, count(title) as count FROM db GROUP BY date<br />
then each time you get a row, you can display the date and the count (number of titles for that date)</p>
<p>like:<br />
11/20/2002 &#8211; count:2<br />
11/21/2002 &#8211; count: 3</p>
<p>And here is where (I think) I get lost,<br />
in your last example:<br />
SELECT date, title, count(title) as count FROM db GROUP BY date<br />
then each time you get a row you can display the date, and the count (number of titles for that date) AND(?) the title&#8230; so again, we&#039;re back to showing mulitple dates? so displaying the results could look like:</p>
<p>11/20/2002 &#8211; This is a title &#8211; count: 2<br />
11/20/2002 &#8211; This is another title &#8211; count: 2</p>
<p>11/21/2002 &#8211; This is a new title &#8211; count: 3<br />
11/21/2002 &#8211; This is yet a new title &#8211; count: 3<br />
11/21/2002 &#8211; And our last title &#8211; count: 3</p>
<p>Is that correct?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Gallaher</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8556</link>
		<dc:creator>Steven Gallaher</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8556</guid>
		<description>DISCLAIMER: I have not done more than skim the above comments.  I do not think what I will say is listed there.  If it is, my apologies.

As mentioned, in SQL &quot;GROUP BY&quot; is used to aggregate information.  Several examples were mentioned.  (I find COUNT, SUM, and MAX to be the most useful.)  What was not mentioned is this:

If all variables other than those in the GROUP BY section are aggregated in the SELECT section, then everything works the way you think.  If, however, you include other variables, then SQL creates the requested aggregate variables and then merges them back.  An example will make this clearer.

If you have a database with date and title and did this:

SELECT date, count(title) as count
FROM db
GROUP BY date

you would get a list of dates with the number of titles for each date.

If you did this instead

SELECT date, title, count(title) as count
FROM db
GROUP BY date

you would, instead get your original table with a new column containing the daily count of titles.  You could get the same result by running the first statement and then another which joined the result back to the original table (by date).

As far as the ORDER BY part: it simply takes the final output and sorts it.</description>
		<content:encoded><![CDATA[<p>DISCLAIMER: I have not done more than skim the above comments.  I do not think what I will say is listed there.  If it is, my apologies.</p>
<p>As mentioned, in SQL &#034;GROUP BY&#034; is used to aggregate information.  Several examples were mentioned.  (I find COUNT, SUM, and MAX to be the most useful.)  What was not mentioned is this:</p>
<p>If all variables other than those in the GROUP BY section are aggregated in the SELECT section, then everything works the way you think.  If, however, you include other variables, then SQL creates the requested aggregate variables and then merges them back.  An example will make this clearer.</p>
<p>If you have a database with date and title and did this:</p>
<p>SELECT date, count(title) as count<br />
FROM db<br />
GROUP BY date</p>
<p>you would get a list of dates with the number of titles for each date.</p>
<p>If you did this instead</p>
<p>SELECT date, title, count(title) as count<br />
FROM db<br />
GROUP BY date</p>
<p>you would, instead get your original table with a new column containing the daily count of titles.  You could get the same result by running the first statement and then another which joined the result back to the original table (by date).</p>
<p>As far as the ORDER BY part: it simply takes the final output and sorts it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jennifer</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8555</link>
		<dc:creator>Jennifer</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8555</guid>
		<description>Well, I think I have a better understanding of the group by thing. I&#039;m sure doing some tests on a real database will clarify further, but I had to get this far so I had a *vague* idea of what I was testing...</description>
		<content:encoded><![CDATA[<p>Well, I think I have a better understanding of the group by thing. I&#039;m sure doing some tests on a real database will clarify further, but I had to get this far so I had a *vague* idea of what I was testing&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mad Bull</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8553</link>
		<dc:creator>Mad Bull</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8553</guid>
		<description>I agree with Lynda on the last comment. Use telnet or ssh and actually run these queries and look at the results, it will all become clear then. Also, it might be a good idea to downloat mysql and load it on your pc for practicing.</description>
		<content:encoded><![CDATA[<p>I agree with Lynda on the last comment. Use telnet or ssh and actually run these queries and look at the results, it will all become clear then. Also, it might be a good idea to downloat mysql and load it on your pc for practicing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Russ</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8554</link>
		<dc:creator>Russ</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8554</guid>
		<description>Could you please direct me to the &quot;How To&quot; location on how to do the LINK, BOLD and ITALICS buttons.

What about the &quot;Email this Entry&quot; feature? Is that a standard MT feature?</description>
		<content:encoded><![CDATA[<p>Could you please direct me to the &#034;How To&#034; location on how to do the LINK, BOLD and ITALICS buttons.</p>
<p>What about the &#034;Email this Entry&#034; feature? Is that a standard MT feature?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lynda</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8552</link>
		<dc:creator>lynda</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8552</guid>
		<description>Jennifer, are you doing any mySQL commands through telnet or ssh?  If you&#039;re not, I recommend you login to your mysql database through a telnet or ssh prompt and do some select statements, etc to see what is returned.  

Doing things that way will probably help you better understand both the order by and group by keywords.  It&#039;s kind of difficult to visualize what is being returned by just putting the mysql statements into php and hoping for the best. :)</description>
		<content:encoded><![CDATA[<p>Jennifer, are you doing any mySQL commands through telnet or ssh?  If you&#039;re not, I recommend you login to your mysql database through a telnet or ssh prompt and do some select statements, etc to see what is returned.  </p>
<p>Doing things that way will probably help you better understand both the order by and group by keywords.  It&#039;s kind of difficult to visualize what is being returned by just putting the mysql statements into php and hoping for the best. <img src='http://www.scriptygoddess.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Haspel</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8551</link>
		<dc:creator>Aaron Haspel</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8551</guid>
		<description>COUNT(*) (as well as COUNT(any_column)) brings back the total number of returned rows, yes, but only in the absence of a GROUP BY clause. 

So in your sample query COUNT(source), and COUNT(category) would all return the total number of rows, i.e., the same number. That&#039;s not what you want. But in Lynda&#039;s query the COUNT() function is applied over the groups that are included in the GROUP BY clause, and that is what you want (I think). 

GROUP BY just applies aggregate functions to the individual groups that are specified. That&#039;s its only reason for living.</description>
		<content:encoded><![CDATA[<p>COUNT(*) (as well as COUNT(any_column)) brings back the total number of returned rows, yes, but only in the absence of a GROUP BY clause. </p>
<p>So in your sample query COUNT(source), and COUNT(category) would all return the total number of rows, i.e., the same number. That&#039;s not what you want. But in Lynda&#039;s query the COUNT() function is applied over the groups that are included in the GROUP BY clause, and that is what you want (I think). </p>
<p>GROUP BY just applies aggregate functions to the individual groups that are specified. That&#039;s its only reason for living.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lynda</title>
		<link>http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/comment-page-1/#comment-8549</link>
		<dc:creator>lynda</dc:creator>
		<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.scriptygoddess.com/archives/2002/11/15/mysql-statements-order-by-and-group-by/#comment-8549</guid>
		<description>Oh, phooey.  Ignore the &quot;graphical&quot;  I was trying to show what the result would look like, but it just didn&#039;t work out. :)</description>
		<content:encoded><![CDATA[<p>Oh, phooey.  Ignore the &#034;graphical&#034;  I was trying to show what the result would look like, but it just didn&#039;t work out. <img src='http://www.scriptygoddess.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

