<?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>生活在远方 &#187; 统计</title>
	<atom:link href="http://www.rsywx.net/wordpress/tag/%e7%bb%9f%e8%ae%a1/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rsywx.net/wordpress</link>
	<description>是的，因为真正的生活是在远方</description>
	<lastBuildDate>Thu, 09 Sep 2010 15:50:04 +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>在MySQL中按照当地时间的时段、星期几统计</title>
		<link>http://www.rsywx.net/wordpress/2010/02/28/aggregating-based-on-local-time-weekday-in-mysql/</link>
		<comments>http://www.rsywx.net/wordpress/2010/02/28/aggregating-based-on-local-time-weekday-in-mysql/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 09:25:55 +0000</pubDate>
		<dc:creator>tr</dc:creator>
				<category><![CDATA[编程、软件、技术]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[local]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[timezone]]></category>
		<category><![CDATA[当地时间]]></category>
		<category><![CDATA[时区]]></category>
		<category><![CDATA[统计]]></category>

		<guid isPermaLink="false">http://www.rsywx.net/wordpress/?p=2135</guid>
		<description><![CDATA[缘起 BT群创建了一个基于status.net的话痨群。我一时手痒，做了个后台，进行一个基本的统计。比如：一天24个时段中每个时段的话痨数量、一周7天中每天的话痨数量等。 这些统计都是要用到基于时间的统计。 我先看了一下后台数据库，我们所使用的这个服务程序将“话痨”贴存放在notice表格中，用字段created来记录。不过它记录的是GMT时间（或者说UTC）时间。这个时间和北京时间存在着8个小时的时差，会造成时段统计的偏移和周内统计的偏移。例如，我在20号上午7点发的话痨，会被储存为19号23时发布的。 一开始，我是用调整服务程序的时区来解决的。虽然在部分程度上解决了“本站点”的问题，但是造成了其他各位BT使用的客户端无法正确获得时间的问题。这个后果是严重的。 解决方案 解决方案只能从MySQL里面去找。通过查阅MySQL参考手册，发现了这么个函数： CONVERT_TZ(dt,from_tz,to_tz) 通过这个函数就可以将一个时区的时间转换到另外一个时间。 于是，相应的按照时段统计的SQL语句就是： SELECT count&#40;*&#41; nc, hour&#40;convert_tz&#40;created, '$stz','$etz' &#41;&#41; dh FROM notice GROUP BY dh 其中的$stz和$etz是两个时区的时间偏移。在本程序中，用的是&#8217;+00:00&#8242;和&#8217;+08:00&#8242;。 经过调试，成功！ 题外话 在CodeIgniter中，如果使用内置的helper函数来生成SQL语句，那么会不成功。提示生成的SQL有错误，或者是找不到&#8217;+00:00&#8242;这个字段。 解决方法是，改写model中的查询过程为： public function getNoticeCountHour&#40;&#41; &#123; $stz='+00:00'; $etz='+08:00'; &#160; $selectsql=&#34;select count(*) nc, hour(convert_tz(created, '$stz','$etz' )) &#8230; <a href="http://www.rsywx.net/wordpress/2010/02/28/aggregating-based-on-local-time-weekday-in-mysql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>缘起</strong></p>
<p>BT群创建了一个基于status.net的话痨群。我一时手痒，做了个后台，进行一个基本的统计。比如：一天24个时段中每个时段的话痨数量、一周7天中每天的话痨数量等。</p>
<p>这些统计都是要用到基于时间的统计。</p>
<p>我先看了一下后台数据库，我们所使用的这个服务程序将“话痨”贴存放在notice表格中，用字段created来记录。不过它记录的是GMT时间（或者说UTC）时间。这个时间和北京时间存在着8个小时的时差，会造成时段统计的偏移和周内统计的偏移。例如，我在20号上午7点发的话痨，会被储存为19号23时发布的。</p>
<p>一开始，我是用调整服务程序的时区来解决的。虽然在部分程度上解决了“本站点”的问题，但是造成了其他各位BT使用的客户端无法正确获得时间的问题。这个后果是严重的。</p>
<p><strong>解决方案</strong></p>
<p>解决方案只能从MySQL里面去找。通过查阅MySQL参考手册，发现了这么个函数：</p>
<blockquote><p><a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_convert-tz">CONVERT_TZ(dt,from_tz,to_tz)</a></p></blockquote>
<p>通过这个函数就可以将一个时区的时间转换到另外一个时间。</p>
<p>于是，相应的按照时段统计的SQL语句就是：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> count<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> nc<span style="color: #66cc66;">,</span> hour<span style="color: #66cc66;">&#40;</span>convert_tz<span style="color: #66cc66;">&#40;</span>created<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'$stz'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'$etz'</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> dh <span style="color: #993333; font-weight: bold;">FROM</span> notice <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> dh</pre></div></div>

<p>其中的$stz和$etz是两个时区的时间偏移。在本程序中，用的是&#8217;+00:00&#8242;和&#8217;+08:00&#8242;。</p>
<p>经过调试，成功！</p>
<p><strong>题外话</strong></p>
<p>在CodeIgniter中，如果使用内置的helper函数来生成SQL语句，那么会不成功。提示生成的SQL有错误，或者是找不到&#8217;+00:00&#8242;这个字段。</p>
<p>解决方法是，改写model中的查询过程为：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getNoticeCountHour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$stz</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'+00:00'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$etz</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'+08:00'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$selectsql</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;select count(*) nc, hour(convert_tz(created, '<span style="color: #006699; font-weight: bold;">$stz</span>','<span style="color: #006699; font-weight: bold;">$etz</span>' )) dh from notice group by dh&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//下面的代码无法执行</span>
    <span style="color: #666666; font-style: italic;">//$this-&gt;db-&gt;select(&quot;count( *  ) nc, hour(convert_tz(created, '$stz','$etz' ) ) dh&quot;);</span>
    <span style="color: #666666; font-style: italic;">//$this-&gt;db-&gt;group_by('dh');</span>
    <span style="color: #666666; font-style: italic;">//$q=$this-&gt;db-&gt;get('notice');</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$selectsql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>这个应该是CodeIgniter的问题。暂时没有时间去搞了。</p>
<p>另外，根据MySQL的文档，使用了convert_tz函数后，这个查询将不能被缓存，会影响执行效率。</p>
<div style="float:left;margin:0px 0px 0px 0px;"><a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count" data-url="http://www.rsywx.net/wordpress/2010/02/28/aggregating-based-on-local-time-weekday-in-mysql/"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.rsywx.net/wordpress/2010/02/28/aggregating-based-on-local-time-weekday-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
