<?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: How to count values with MySQL queries</title>
	<atom:link href="http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Sat, 17 Jul 2010 11:08:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Subhee</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-11295</link>
		<dc:creator>Subhee</dc:creator>
		<pubDate>Tue, 09 Feb 2010 01:19:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-11295</guid>
		<description>Thank you.  Great sharing &amp; i have search this type of search long time ago.</description>
		<content:encoded><![CDATA[<p>Thank you.  Great sharing &amp; i have search this type of search long time ago.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-1272</link>
		<dc:creator>mike</dc:creator>
		<pubDate>Thu, 29 Jan 2009 12:07:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-1272</guid>
		<description>thanks a lot.. &#039;group by&#039; did the trick for me.</description>
		<content:encoded><![CDATA[<p>thanks a lot.. &#8216;group by&#8217; did the trick for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Knix</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-866</link>
		<dc:creator>Knix</dc:creator>
		<pubDate>Fri, 28 Nov 2008 04:46:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-866</guid>
		<description>Hi Quentin,

You can just total up the monthly fee field using the MySQL aggregate function &lt;b&gt;SUM&lt;/b&gt;.  Here is an example of the usage assuming the monthly fee field is just called &lt;b&gt;fee&lt;/b&gt; and your table is called &lt;b&gt;members&lt;/b&gt;:

&lt;pre lang=&quot;mysql&quot;&gt;
SELECT SUM(fee) as total_sum
FROM members;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi Quentin,</p>
<p>You can just total up the monthly fee field using the MySQL aggregate function <b>SUM</b>.  Here is an example of the usage assuming the monthly fee field is just called <b>fee</b> and your table is called <b>members</b>:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> SUM<span style="color: #66cc66;">&#40;</span>fee<span style="color: #66cc66;">&#41;</span> as total_sum
<span style="color: #993333; font-weight: bold;">FROM</span> members;</pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Quentin Nell</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-841</link>
		<dc:creator>Quentin Nell</dc:creator>
		<pubDate>Sun, 23 Nov 2008 12:22:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-841</guid>
		<description>Hi, i have a membership site iam working on... tables are as follows ID,name,email and monthly fee. now i don&#039;t want to count the total records, i would like to count and add the totals of each monthly fee record to see what is the total income of all the members &quot;Records&quot; are.</description>
		<content:encoded><![CDATA[<p>Hi, i have a membership site iam working on&#8230; tables are as follows ID,name,email and monthly fee. now i don&#8217;t want to count the total records, i would like to count and add the totals of each monthly fee record to see what is the total income of all the members &#8220;Records&#8221; are.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Knix</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-683</link>
		<dc:creator>Knix</dc:creator>
		<pubDate>Wed, 12 Nov 2008 19:09:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-683</guid>
		<description>No problem =)</description>
		<content:encoded><![CDATA[<p>No problem =)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evin</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-682</link>
		<dc:creator>Evin</dc:creator>
		<pubDate>Wed, 12 Nov 2008 18:24:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-682</guid>
		<description>this did the trick!

Thanks Knix</description>
		<content:encoded><![CDATA[<p>this did the trick!</p>
<p>Thanks Knix</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evin</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-681</link>
		<dc:creator>Evin</dc:creator>
		<pubDate>Wed, 12 Nov 2008 17:39:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-681</guid>
		<description>nope, it just keeps counting how many values in total not grouped

$row[&#039;total&#039;]? where is this defined?</description>
		<content:encoded><![CDATA[<p>nope, it just keeps counting how many values in total not grouped</p>
<p>$row['total']? where is this defined?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Knix</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-680</link>
		<dc:creator>Knix</dc:creator>
		<pubDate>Wed, 12 Nov 2008 17:23:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-680</guid>
		<description>Hi Evin,  

The echo statement should also include your count field.  Add an alias to the count and use that in the echo:

&lt;pre lang=&quot;php&quot;&gt;
$query2 = (”SELECT sem_id, count(*) as total FROM `attendees` GROUP BY sem_id”);
&lt;/pre&gt;

&lt;pre lang=&quot;php&quot;&gt;
echo &quot;sem_id: &quot; . $row[&#039;sem_id&#039;] . &quot; total: &quot; . $row[&#039;total&#039;] . &quot;&lt;br /&gt;&quot;;
&lt;/pre&gt;

I would run the raw query directly in the MySQL command line first to see if it works.  This way, you do not need to worry about PHP just yet.  Your query looks good and should work.</description>
		<content:encoded><![CDATA[<p>Hi Evin,  </p>
<p>The echo statement should also include your count field.  Add an alias to the count and use that in the echo:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$query2</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>”SELECT sem_id<span style="color: #339933;">,</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> total FROM `attendees` GROUP BY sem_id”<span style="color: #009900;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;sem_id: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="">'sem_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; total: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="">'total'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span>;</pre></div></div>

<p>I would run the raw query directly in the MySQL command line first to see if it works.  This way, you do not need to worry about PHP just yet.  Your query looks good and should work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evin</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-679</link>
		<dc:creator>Evin</dc:creator>
		<pubDate>Wed, 12 Nov 2008 16:06:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-679</guid>
		<description>this is what i have, had it in a while loop now its in the &quot;if&quot; statement and it just counts all values in the column together without distinguishing or grouping values that are the same

 $db = mysqli_connect( &quot;localhost&quot; , &quot;tester&quot;, &quot;123456&quot; , &quot;tester&quot; )  
or die (&quot;not able to connect.&quot;);
   
  
  $query2 = (&quot;SELECT sem_id, count(*) FROM `attendees` GROUP BY sem_id&quot;);
  $result2 = mysqli_query($db , $query2);
  
if ($row = mysqli_fetch_assoc($result2 ))
   
{

echo $row[sem_id];
 

};</description>
		<content:encoded><![CDATA[<p>this is what i have, had it in a while loop now its in the &#8220;if&#8221; statement and it just counts all values in the column together without distinguishing or grouping values that are the same</p>
<p> $db = mysqli_connect( &#8220;localhost&#8221; , &#8220;tester&#8221;, &#8220;123456&#8243; , &#8220;tester&#8221; )<br />
or die (&#8220;not able to connect.&#8221;);</p>
<p>  $query2 = (&#8220;SELECT sem_id, count(*) FROM `attendees` GROUP BY sem_id&#8221;);<br />
  $result2 = mysqli_query($db , $query2);</p>
<p>if ($row = mysqli_fetch_assoc($result2 ))</p>
<p>{</p>
<p>echo $row[sem_id];</p>
<p>};</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Knix</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-678</link>
		<dc:creator>Knix</dc:creator>
		<pubDate>Wed, 12 Nov 2008 16:03:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-678</guid>
		<description>Hi Evin,

It almost seems like the GROUP BY modifier was not in the query.

Can you please give me the schema of your table so I can duplicate it?</description>
		<content:encoded><![CDATA[<p>Hi Evin,</p>
<p>It almost seems like the GROUP BY modifier was not in the query.</p>
<p>Can you please give me the schema of your table so I can duplicate it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evin</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-677</link>
		<dc:creator>Evin</dc:creator>
		<pubDate>Wed, 12 Nov 2008 15:56:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-677</guid>
		<description>this actually just gives me 

56
34
23
89
56
56
56
34
09
09</description>
		<content:encoded><![CDATA[<p>this actually just gives me </p>
<p>56<br />
34<br />
23<br />
89<br />
56<br />
56<br />
56<br />
34<br />
09<br />
09</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Knix</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-675</link>
		<dc:creator>Knix</dc:creator>
		<pubDate>Wed, 12 Nov 2008 04:37:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-675</guid>
		<description>You can use the &lt;b&gt;GROUP BY&lt;/b&gt; modifier and just count the number of rows =)

&lt;pre line=&quot;1&quot; lang=&quot;mysql&quot;&gt;
SELECT sem_id, count(*) FROM `name_of_your_table` GROUP BY sem_id
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>You can use the <b>GROUP BY</b> modifier and just count the number of rows =)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mysql mysql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> sem_id, count<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> `name_of_your_table` <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> sem_id</pre></td></tr></table></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Evin</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-673</link>
		<dc:creator>Evin</dc:creator>
		<pubDate>Wed, 12 Nov 2008 02:54:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-673</guid>
		<description>but what if you want to count an &quot;int&quot; of different values... for example in column &quot;sem_id&quot; there is 

56
34
23
89
56
56
56
34
09
09

and you want to count how many of &quot;09&quot; there are and how many of &quot;56&quot; there are...</description>
		<content:encoded><![CDATA[<p>but what if you want to count an &#8220;int&#8221; of different values&#8230; for example in column &#8220;sem_id&#8221; there is </p>
<p>56<br />
34<br />
23<br />
89<br />
56<br />
56<br />
56<br />
34<br />
09<br />
09</p>
<p>and you want to count how many of &#8220;09&#8243; there are and how many of &#8220;56&#8243; there are&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
