<?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 for Web Developers</description>
	<lastBuildDate>Thu, 19 Jan 2012 11:30:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: Cim</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-62568</link>
		<dc:creator>Cim</dc:creator>
		<pubDate>Sat, 10 Dec 2011 22:16:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-62568</guid>
		<description>Thanks for great post. Just would like to ask if its possible to to find the difference between yes votes and no votes for each candidate for example obama has 4 yes votes and 2 no votes. so i would like to get 4-2 is this possible in the query itself? Thanks.</description>
		<content:encoded><![CDATA[<p>Thanks for great post. Just would like to ask if its possible to to find the difference between yes votes and no votes for each candidate for example obama has 4 yes votes and 2 no votes. so i would like to get 4-2 is this possible in the query itself? Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-56158</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 24 Nov 2011 11:39:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-56158</guid>
		<description>Thank buddy it helps me alot. :)</description>
		<content:encoded><![CDATA[<p>Thank buddy it helps me alot. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Allen Liu</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-47391</link>
		<dc:creator>Allen Liu</dc:creator>
		<pubDate>Sun, 16 Oct 2011 18:41:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-47391</guid>
		<description>Hi Mark,

Thanks for your input!

Allen</description>
		<content:encoded><![CDATA[<p>Hi Mark,</p>
<p>Thanks for your input!</p>
<p>Allen</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark H</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-47383</link>
		<dc:creator>Mark H</dc:creator>
		<pubDate>Sun, 16 Oct 2011 18:05:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-47383</guid>
		<description>Thank you so much for this. This is EXACTLY what I was looking for. FYI if you want to do something where you sort by the total tally of upvotes and downvotes (with positive numbers at the top, 0s in the middle, and negative numbers at the bottom), try this:

SELECT (SUM(IF(vote = 1, 1,0)) - SUM(IF(vote = 0, 1,0))) AS consensus
FROM votes
GROUP BY person ORDER BY consensus DESC</description>
		<content:encoded><![CDATA[<p>Thank you so much for this. This is EXACTLY what I was looking for. FYI if you want to do something where you sort by the total tally of upvotes and downvotes (with positive numbers at the top, 0s in the middle, and negative numbers at the bottom), try this:</p>
<p>SELECT (SUM(IF(vote = 1, 1,0)) &#8211; SUM(IF(vote = 0, 1,0))) AS consensus<br />
FROM votes<br />
GROUP BY person ORDER BY consensus DESC</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve 10-10-11 &#124; emerginsttech</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-46122</link>
		<dc:creator>Steve 10-10-11 &#124; emerginsttech</dc:creator>
		<pubDate>Mon, 10 Oct 2011 22:17:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-46122</guid>
		<description>[...] get to the point of being able to graph, tallies must be made from the values in the database.  I&#8217;ll first try this method and if that doesn&#8217;t work, I will try this [...]</description>
		<content:encoded><![CDATA[<p>[...] get to the point of being able to graph, tallies must be made from the values in the database.  I&#8217;ll first try this method and if that doesn&#8217;t work, I will try this [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tallman</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-37073</link>
		<dc:creator>tallman</dc:creator>
		<pubDate>Tue, 12 Jul 2011 11:35:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-37073</guid>
		<description>Thanks ..! It helped me a lot</description>
		<content:encoded><![CDATA[<p>Thanks ..! It helped me a lot</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-31145</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 20 Apr 2011 03:53:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-31145</guid>
		<description>Thank you,good stuff</description>
		<content:encoded><![CDATA[<p>Thank you,good stuff</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TheCloser</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-22990</link>
		<dc:creator>TheCloser</dc:creator>
		<pubDate>Mon, 10 Jan 2011 21:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-22990</guid>
		<description>Thank you very much for posting this! My SQL is a bit rusty and was stuck on a query quite similar to this.</description>
		<content:encoded><![CDATA[<p>Thank you very much for posting this! My SQL is a bit rusty and was stuck on a query quite similar to this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: MySQL: contare le occorrenze tra i valori di un campo</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/comment-page-1/#comment-22505</link>
		<dc:creator>MySQL: contare le occorrenze tra i valori di un campo</dc:creator>
		<pubDate>Mon, 03 Jan 2011 10:13:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.randomsnippets.com/?p=116#comment-22505</guid>
		<description>[...] in quel ..SUM(IF.. che ci evita di creare un loop in PHP a valle della query per il conteggio.via Random Snippets google_ad_client = &quot;pub-4493845777744239&quot;; /* 234x60, creato 19/09/08 */ google_ad_slot = [...]</description>
		<content:encoded><![CDATA[<p>[...] in quel ..SUM(IF.. che ci evita di creare un loop in PHP a valle della query per il conteggio.via Random Snippets google_ad_client = &quot;pub-4493845777744239&quot;; /* 234&#215;60, creato 19/09/08 */ google_ad_slot = [...]</p>
]]></content:encoded>
	</item>
	<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" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">SUM</span><span style="color: #FF00FF;">&#40;</span>fee<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> total_sum
<span style="color: #990099; font-weight: bold;">FROM</span> members<span style="color: #000033;">;</span></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" 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><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">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="color: #0000ff;">'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="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</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" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> sem_id<span style="color: #000033;">,</span> <span style="color: #000099;">count</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`name<span style="color: #008080; font-weight: bold;">_</span>of<span style="color: #008080; font-weight: bold;">_</span>your<span style="color: #008080; font-weight: bold;">_</span>table`</span> <span style="color: #990099; font-weight: bold;">GROUP 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>

