<?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>Java豆技术站点 &#187; count</title>
	<atom:link href="http://javadou.com/tag/count/feed/" rel="self" type="application/rss+xml" />
	<link>http://javadou.com</link>
	<description>Java</description>
	<lastBuildDate>Thu, 12 Aug 2010 09:01:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>深入浅出 Group by和Having</title>
		<link>http://javadou.com/group-by-having-in-sql-290/</link>
		<comments>http://javadou.com/group-by-having-in-sql-290/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 00:05:00 +0000</pubDate>
		<dc:creator>阿超</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[groupby]]></category>
		<category><![CDATA[having]]></category>
		<category><![CDATA[sum]]></category>

		<guid isPermaLink="false">http://javadou.com/group-by-having-in-sql-290/</guid>
		<description><![CDATA[在介绍GROUP BY 和 HAVING 子句前，我们必需先讲讲sql语言中一种特殊的函数：聚合函数，例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根本区别就是它们一般作用在多条记录上。 

SELECT SUM(population) FROM bbc

　　这里的S... ]]></description>
			<content:encoded><![CDATA[<p>在介绍GROUP BY 和 HAVING 子句前，我们必需先讲讲sql语言中一种特殊的函数：聚合函数，例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根本区别就是它们一般作用在多条记录上。 </p>
<p>SELECT SUM(population) FROM bbc</p>
<p> 这里的SUM作用在所有返回记录的population字段上，结果就是该查询只返回一个结果，即所有国家的总人口数。   <br /> 通过使用GROUP BY 子句，可以让SUM 和 COUNT 这些函数对属于一组的数据起作用。当你指定 GROUP BY region 时， 属于同一个region（地区）的一组数据将只能返回一行值，也就是说，表中所有除region（地区）外的字段，只能通过 SUM, COUNT等聚合函数运算后返回一个值。    <br /> HAVING子句可以让我们筛选成组后的各组数据，WHERE子句在聚合前先筛选记录．也就是说作用在GROUP BY 子句和HAVING子句前．    <br />而 HAVING子句在聚合后对组记录进行筛选。    <br /> 让我们还是通过具体的实例来理解GROUP BY 和 HAVING 子句，还采用第三节介绍的bbc表。    <br /> SQL实例：    <br /> 一、显示每个地区的总人口数和总面积：</p>
<p>SELECT region, SUM(population), SUM(area)   <br />FROM bbc    <br />GROUP BY region</p>
<p> 先以region把返回记录分成多个组，这就是GROUP BY的字面含义。分完组后，然后用聚合函数对每组中的不同字段（一或多条记录）作运算。   <br /> 二、 显示每个地区的总人口数和总面积．仅显示那些面积超过1000000的地区。</p>
<p>SELECT region, SUM(population), SUM(area)   <br />FROM bbc    <br />GROUP BY region    <br />HAVING SUM(area)&gt;1000000</p>
<p> 在这里，我们不能用where来筛选超过1000000的地区，因为表中不存在这样一条记录。   <br /> 相反，HAVING子句可以让我们筛选成组后的各组数据．</p>
]]></content:encoded>
			<wfw:commentRss>http://javadou.com/group-by-having-in-sql-290/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL中以count及sum为条件的查询</title>
		<link>http://javadou.com/sql-count-sum-select-289/</link>
		<comments>http://javadou.com/sql-count-sum-select-289/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 05:01:00 +0000</pubDate>
		<dc:creator>阿超</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[sum]]></category>

		<guid isPermaLink="false">http://javadou.com/sql-count-sum-select-289/</guid>
		<description><![CDATA[在开发时，我们经常会遇到以“累计（count）”或是“累加（sum）”为条件的查询。比如user_num表... ]]></description>
			<content:encoded><![CDATA[<p>在开发时，我们经常会遇到以“<strong>累计（count）</strong>”或是“<strong>累加（sum）</strong>”为条件的查询。比如user_num表：</p>
<div>
<table style="width: 100px; height: 100px" cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td>
<div align="center">id</div>
</td>
<td>
<div align="center">user</div>
</td>
<td>
<div align="center">num</div>
</td>
</tr>
<tr>
<td>
<div align="center">1</div>
</td>
<td>
<div align="center">a</div>
</td>
<td>
<div align="center">3</div>
</td>
</tr>
<tr>
<td>
<div align="center">2</div>
</td>
<td>
<div align="center">a</div>
</td>
<td>
<div align="center">4</div>
</td>
</tr>
<tr>
<td>
<div align="center">3</div>
</td>
<td>
<div align="center">b</div>
</td>
<td>
<div align="center">5</div>
</td>
</tr>
<tr>
<td>
<div align="center">4</div>
</td>
<td>
<div align="center">b</div>
</td>
<td>
<div align="center">7</div>
</td>
</tr>
</tbody>
</table></div>
<div>&#160;</div>
<p>例１：查询出现过2次的user。</p>
<p>往往初学者会错误地认为在where 语句里直接使用count()算法，很显然这个想法是错误的，count()方法并不能被用在where子句中，为了解决问题，我们可以在group by子句后面使用HAVING来做条件限制。</p>
<p>错误做法：select * from user_num where count(user)&gt;＝2 group by user;</p>
<p>正确做法：select * from user_num group by user HAVING count(user)&gt;＝2 ;</p>
<p>解释说明：HAVING 与 WHERE 类似，可用来决定选择哪些记录。HAVING 子句在SELECT语句中指定，显示哪些已用 GROUP BY 子句分组的记录。在GROUP BY组合了记录后， HAVING会显示 GROUP BY 子句分组的任何符合 HAVING 子句的记录。</p>
<p>例２：查询单一用户的num总和大于10的用户。</p>
<p>有前面的经验，把sum()方法写在HAVING子句中。</p>
<p>正确做法：select * from user_num group by user HAVING sum(num)&gt;10 ;</p>
<p><font color="#800000">注意：一个HAVING子句最多只能包含40个表达式，HAVING子句的表达式之间可以用AND和OR分割。</font></p>
]]></content:encoded>
			<wfw:commentRss>http://javadou.com/sql-count-sum-select-289/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL优化之COUNT(*)效率</title>
		<link>http://javadou.com/mysql-count-youhua-262/</link>
		<comments>http://javadou.com/mysql-count-youhua-262/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 18:05:00 +0000</pubDate>
		<dc:creator>阿超</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://javadou.com/mysql-count-youhua-262/</guid>
		<description><![CDATA[刚给一个朋友解决他写的Discuz!插件的问题，说到MySQL的COUNT(*)的效率，发现越说越说不清楚，干脆写下来，分享给大家... ]]></description>
			<content:encoded><![CDATA[<p>刚给一个朋友解决他写的Discuz!插件的问题，说到MySQL的COUNT(*)的效率，发现越说越说不清楚，干脆写下来，分享给大家。</p>
<p><strong>COUNT(*)与COUNT(COL)</strong>    <br />网上搜索了下，发现各种说法都有：    <br />比如认为COUNT(COL)比COUNT(*)快的；    <br />认为COUNT(*)比COUNT(COL)快的；    <br />还有朋友很搞笑的说到这个其实是看人品的。</p>
<p>在<strong>不加WHERE限制</strong>条件的情况下，COUNT(*)与COUNT(COL)基本可以认为是等价的；    <br />但是在<strong>有WHERE限制</strong>条件的情况下，COUNT(*)会比COUNT(COL)快非常多；</p>
<p>具体的数据参考如下：</p>
<p>mysql&gt; SELECT COUNT(*) FROM cdb_posts where fid = 604;   <br />+————+    <br />| COUNT(fid) |    <br />+————+    <br />| 79000 |    <br />+————+    <br />1 row in set (0.03 sec)</p>
<p>mysql&gt; SELECT COUNT(tid) FROM cdb_posts where fid = 604;   <br />+————+    <br />| COUNT(tid) |    <br />+————+    <br />| 79000 |    <br />+————+    <br />1 row in set (0.33 sec)</p>
<p>mysql&gt; SELECT COUNT(pid) FROM cdb_posts where fid = 604;   <br />+————+    <br />| COUNT(pid) |    <br />+————+    <br />| 79000 |    <br />+————+    <br />1 row in set (0.33 sec)</p>
<p>COUNT(*)通常是对主键进行索引扫描，而COUNT(COL)就不一定了，另外前者是统计表中的所有符合的纪录总数，而后者是计算表中所有符合的COL的纪录数。还有有区别的。</p>
<p><strong>COUNT时的WHERE</strong>    <br />这点以前就写过，详细请看《<a href="http://javadou.com/mysql-describe-if-exit-260/">Mysql中count(*),DISTINCT的使用方法和效率研究</a>》：<a title="http://javadou.com/mysql-describe-if-exit-260/" href="http://javadou.com/mysql-describe-if-exit-260/">http://javadou.com/mysql-describe-if-exit-260/</a></p>
<p>简单说下，就是COUNT的时候，如果没有WHERE限制的话，MySQL直接返回保存有总的行数   <br />而在有WHERE限制的情况下，总是需要对MySQL进行全表遍历。</p>
<p><strong>优化总结</strong>：    <br />1.任何情况下SELECT COUNT(*) FROM tablename是最优选择；    <br />2.尽量减少SELECT COUNT(*) FROM tablename WHERE COL = ‘value’ 这种查询；    <br />3.杜绝SELECT COUNT(COL) FROM tablename WHERE COL2 = ‘value’ 的出现。</p>
]]></content:encoded>
			<wfw:commentRss>http://javadou.com/mysql-count-youhua-262/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
