<?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.d5s.cn/archives/category/develop/database/feed" rel="self" type="application/rss+xml" />
	<link>http://www.d5s.cn</link>
	<description>我喜欢忽悠，她喜欢被忽悠。</description>
	<lastBuildDate>Wed, 16 Nov 2011 15:35:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>3天时间，被垃圾蜘蛛爬虫耗尽了10G的流量</title>
		<link>http://www.d5s.cn/archives/427</link>
		<comments>http://www.d5s.cn/archives/427#comments</comments>
		<pubDate>Sun, 06 Mar 2011 05:29:20 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/?p=427</guid>
		<description><![CDATA[unknown robot (identified by empty user agent string) 10.25 g 从3月1日到3日，仅仅3的时间。 联系主机托管方HomeZZ.com 的管理员，经过查找后，发现主机上其他账户正常，不是主机问题。 查看日志后，发现有来自 208.101.54.6-static.reverse.softlayer.com (208.101.54.6) 的访客，消耗流量最多10g左右，于是禁用ip，目前站点流量正常。 在robots.txt里面也禁禁止了很多垃圾机器人来抓取本站。 这次被攻击，HomeZZ的管理员又给了2G的流量，十分感谢，推荐其他朋友也来这里购买虚拟主机。 这里放出几个邀请码，有需要的自取： https://homezz.com/account/reg.php?c=NO1HA17K https://homezz.com/account/reg.php?c=F1RU0FB2 https://homezz.com/account/reg.php?c=HE050ICN]]></description>
			<content:encoded><![CDATA[<p>unknown robot (identified by empty user agent string)	 10.25 g<br />
从3月1日到3日，仅仅3的时间。</p>
<p>联系主机托管方<a href="https://homezz.com" target="_blank">HomeZZ.com</a> 的管理员，经过查找后，发现主机上其他账户正常，不是主机问题。<br />
查看日志后，发现有来自 208.101.54.6-static.reverse.softlayer.com (208.101.54.6) 的访客，消耗流量最多10g左右，于是禁用ip，目前站点流量正常。</p>
<p>在robots.txt里面也禁禁止了很多垃圾机器人来抓取本站。</p>
<p>这次被攻击，HomeZZ的管理员又给了2G的流量，十分感谢，推荐其他朋友也来这里购买虚拟主机。<br />
这里放出几个邀请码，有需要的自取：</p>
<p><a href="https://homezz.com/account/reg.php?c=NO1HA17K" target="_blank">https://homezz.com/account/reg.php?c=NO1HA17K</a></p>
<p><a href="https://homezz.com/account/reg.php?c=F1RU0FB2" target="_blank">https://homezz.com/account/reg.php?c=F1RU0FB2</a></p>
<p><a href="https://homezz.com/account/reg.php?c=HE050ICN" target="_blank">https://homezz.com/account/reg.php?c=HE050ICN</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/427/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>mysql 中 时间和日期函数</title>
		<link>http://www.d5s.cn/archives/422</link>
		<comments>http://www.d5s.cn/archives/422#comments</comments>
		<pubDate>Sat, 12 Feb 2011 12:28:29 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/?p=422</guid>
		<description><![CDATA[转自：http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.html 一、MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间（date + time）函数：now() mysql&#62; select now(); +&#8211;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+ &#124; now() &#124; +&#8211;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+ &#124; 2008-08-08 22:20:46 &#124; +&#8211;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+ 除了 now() 函数能获得当前的日期时间外，MySQL 中还有下面的函数： current_timestamp() ,current_timestamp ,localtime() ,localtime ,localtimestamp &#8211; (v4.0.6) ,localtimestamp() &#8211; (v4.0.6) 这些日期时间函数，都等同于 now()。鉴于 now() 函数简短易记，建议总是使用 now() 来替代上面列出的函数。 1.2 获得当前日期+时间（date + time）函数：sysdate() sysdate() 日期时间函数跟 now() 类似，不同之处在于：now() 在执行开始时值就得到了， sysdate() 在函数执行时动态得到值。看下面的例子就明白了： mysql&#62; select now(), sleep(3), now(); +&#8211;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+ &#8230; <a href="http://www.d5s.cn/archives/422">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>转自：http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.html</p>
<p><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">一、MySQL 获得当前日期时间 函数</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1.1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 获得当前日期</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">时间（date </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time）函数：now()</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">20</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">46</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
除了 now() 函数能获得当前的日期时间外，MySQL 中还有下面的函数：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">()<br />
,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
,localtime()<br />
,localtime<br />
,localtimestamp </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> (v4.0.6)</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,localtimestamp() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> (v4.0.6)</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
这些日期时间函数，都等同于 now()。鉴于 now() 函数简短易记，建议总是使用 now() 来替代上面列出的函数。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1.2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 获得当前日期</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">时间（date </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time）函数：sysdate()</p>
<p>sysdate() 日期时间函数跟 now() 类似，不同之处在于：now() 在执行开始时值就得到了， sysdate() 在函数执行时动态得到值。看下面的例子就明白了：</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now(), sleep(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">), now();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> sleep(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">28</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">21</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">0</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">28</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">21</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> sysdate(), sleep(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">), sysdate();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> sysdate() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> sleep(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> sysdate() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">28</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">41</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">0</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">28</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">44</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
可以看到，虽然中途 sleep </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 秒，但 now() 函数两次的时间值是相同的； sysdate() 函数两次得到的时间值相差 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 秒。MySQL Manual 中是这样描述 sysdate() 的：</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">Return</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> the time at which the </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">function</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> executes。</p>
<p>sysdate() 日期时间函数，一般情况下很少用到。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. 获得当前日期（date）函数：curdate()</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> curdate();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> curdate() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
其中，下面的两个日期函数等同于 curdate()：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">current_date</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">()<br />
,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">current_date</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"></p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. 获得当前时间（time）函数：curtime()</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> curtime();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> curtime() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">41</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
其中，下面的两个时间函数等同于 curtime()：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">current_time</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">()<br />
,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">current_time</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"></p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">4</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. 获得当前 UTC 日期时间函数：utc_date(), utc_time(), utc_timestamp()</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> utc_timestamp(), utc_date(), utc_time(), now()</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> utc_timestamp() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> utc_date() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> utc_time() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">14</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">47</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">11</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">14</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">47</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">11</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">47</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">11</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
因为我国位于东八时区，所以本地时间 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> UTC 时间 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">8</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 小时。UTC 时间在业务涉及多个国家和地区的时候，非常有用。<br />
二、MySQL 日期时间 Extract（选取） 函数。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. 选取日期时间的各个部分：日期、时间、年、季度、月、日、小时、分钟、秒、微秒</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-09-10 07:15:30.123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">;</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-09-10</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 07:15:30.123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">year</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> quarter(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">month</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 9</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> week(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 36</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 10</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> hour(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> minute(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 15</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> second(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> microsecond(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL Extract() 函数，可以上面实现类似的功能：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-09-10 07:15:30.123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">;</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">year</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(quarter </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">month</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 9</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(week </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 36</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 10</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(hour </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(minute </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 15</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(second </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(microsecond </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(year_month </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 200809</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(day_hour </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 1007</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(day_minute </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 100715</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(day_second </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 10071530</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(day_microsecond </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 10071530123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(hour_minute </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 715</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(hour_second </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 71530</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(hour_microsecond </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 71530123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(minute_second </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 1530</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(minute_microsecond </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 1530123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> extract(second_microsecond </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">from</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 30123456</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL Extract() 函数除了没有date(),time() 的功能外，其他功能一应具全。并且还具有选取‘day_microsecond’ 等功能。注意这里不是只选取 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 和 microsecond，而是从日期的 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 部分一直选取到 microsecond 部分。够强悍的吧！</p>
<p>MySQL Extract() 函数唯一不好的地方在于：你需要多敲几次键盘。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL dayof… 函数：dayofweek(), dayofmonth(), dayofyear()</p>
<p>分别返回日期参数，在一周、一月、一年中的位置。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">;</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> dayofweek(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 6</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> dayofmonth(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 8</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> dayofyear(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 221</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
日期 ‘</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">′ 是一周中的第 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">6</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 天（</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Sunday, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Monday, …, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Saturday）；一月中的第 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">8</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 天；一年中的第 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">221</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 天。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">4</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL week… 函数：week(), weekofyear(), dayofweek(), weekday(), yearweek()</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">;</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> week(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 31</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> week(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 32</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> weekofyear(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 32</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> dayofweek(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 6</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> weekday(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 4</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> yearweek(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 200831</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL week() 函数，可以有两个参数，具体可看手册。 weekofyear() 和 week() 一样，都是计算“某天”是位于一年中的第几周。 weekofyear(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) 等价于 week(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)。</p>
<p>MySQL weekday() 函数和 dayofweek() 类似，都是返回“某天”在一周中的位置。不同点在于参考的标准， weekday：(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">0</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Monday, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Tuesday, …, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">6</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Sunday)； dayofweek：（</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Sunday, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Monday, …, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Saturday）</p>
<p>MySQL yearweek() 函数，返回 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">year</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> week 位置(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">31</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">5</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 返回星期和月份名称函数：dayname(), monthname()</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">;</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> dayname(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> Friday</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> monthname(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> August</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
思考，如何返回中文的名称呢？</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">6</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL last_day() 函数：返回月份中的最后一天。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> last_day(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-02-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-02-29</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> last_day(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-31</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL last_day() 函数非常有用，比如我想得到当前月份中有多少天，可以这样来计算：</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now(), </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(last_day(now())) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">as</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> days;</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> days </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">11</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">45</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">45</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">31</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
三、MySQL 日期时间计算函数</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 为日期增加一个时间间隔：date_add()</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> now();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> add 1 day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> hour); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> add 1 hour</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> minute); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> <img style="border: 0px initial initial;" src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> second);<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> microsecond);<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> week);<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">month</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">);<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> quarter);<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">year</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> sub 1 day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL adddate(), addtime()函数，可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例：</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">set</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">=</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-09 12:12:33</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">;</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">01:15:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> hour_second);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">01:15:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> hour_second) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">13</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">28</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">03</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">1 01:15:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> day_second);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">1 01:15:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> day_second) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">10</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">13</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">28</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">03</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
date_add() 函数，分别为 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008000;">@dt</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> 增加了“1小时 15分 30秒” 和 “1天 1小时 15分 30秒”。建议：总是使用 date_add() 日期时间函数来替代 adddate(), addtime()。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 为日期减去一个时间间隔：date_sub()</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_sub(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">1998-01-01 00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">1 1:1:1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> day_second);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_sub(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">1998-01-01 00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">1 1:1:1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> day_second) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1997</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">12</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">58</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">59</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL date_sub() 日期时间函数 和 date_add() 用法一致，不再赘述。另外，MySQL 中还有两个函数 subdate(), subtime()，建议，用 date_sub() 来替代。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 另类日期函数：period_add(P,N), period_diff(P1,P2)</p>
<p>函数参数“P” 的格式为“YYYYMM” 或者 “YYMM”，第二个参数“N” 表示增加或减去 N </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">month</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">（月）。</p>
<p>MySQL period_add(P,N)：日期加</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">/</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">减去N月。</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> period_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200808</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">), period_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">20080808</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> period_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200808</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> period_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">20080808</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200810</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">20080806</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL period_diff(P1,P2)：日期 P1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">P2，返回 N 个月。</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> period_diff(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200808</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200801</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> period_diff(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200808</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">200801</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
在 MySQL 中，这两个日期函数，一般情况下很少用到。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">4</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 日期、时间相减函数：</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(date1,date2), timediff(time1,time2)</p>
<p>MySQL </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(date1,date2)：两个日期相减 date1 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date2，返回天数。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> -7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL timediff(time1,time2)：两个日期相减 time1 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time2，返回 time 差值。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timediff(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 08:08:08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 08:08:08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timediff(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">08:08:08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 08:08:08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
注意：timediff(time1,time2) 函数的两个参数类型必须相同。<br />
四、MySQL 日期转换函数、时间转换函数</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL （时间、秒）转换函数：time_to_sec(time), sec_to_time(seconds)</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time_to_sec(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">01:00:05</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 3605</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> sec_to_time(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3605</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;01:00:05&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL （日期、天数）转换函数：to_days(date), from_days(days)</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> to_days(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">0000-00-00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 0</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> to_days(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 733627</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> from_days(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">0</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;0000-00-00&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> from_days(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">733627</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2008-08-08&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">Str</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">to</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Date （字符串转换为日期）函数：str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">str</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, format)</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">08/09/2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%m/%d/%Y</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">08/09/08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> , </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%m/%d/%y</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">08.09.2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%m.%d.%Y</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">08:09:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%h:%i:%s</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 08:09:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">08.09.2008 08:09:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%m.%d.%Y %h:%i:%s</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-09 08:09:30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
可以看到，str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">str</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,format) 转换函数，可以把一些杂乱无章的字符串转换为日期格式。另外，它也可以转换为时间。“format” 可以参看 MySQL 手册。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">4</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL Date</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">/</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">Time </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">to</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">Str</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">（日期</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">/</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">时间转换为字符串）函数：date_format(date,format), time_format(time,format)</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 22:23:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%W %M %Y</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 22:23:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%W %M %Y</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> Friday August </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 22:23:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%Y%m%d%H%i%s</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 22:23:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%Y%m%d%H%i%s</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">20080808222301</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">22:23:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%H.%i.%s</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">);</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> time_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">22:23:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%H.%i.%s</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22.23</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">.</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL 日期、时间转换函数：date_format(date,format), time_format(time,format) 能够把一个日期</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">/</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">时间转换成各种各样的字符串格式。它是 str_to_date(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">str</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,format) 函数的 一个逆转换。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">5</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 获得国家地区时间格式函数：get_format()</p>
<p>MySQL get_format() 语法：</p>
<p>get_format(date</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">time</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">datetime</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">eur</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">usa</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">jis</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">iso</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">internal</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"></p>
<p>MySQL get_format() 用法的全部示例：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(date,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">usa</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%m.%d.%Y&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(date,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">jis</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y-%m-%d&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(date,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">iso</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y-%m-%d&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(date,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">eur</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%d.%m.%Y&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(date,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">internal</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)     ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y%m%d&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">datetime</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">usa</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)      ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y-%m-%d %H.%i.%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">datetime</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">jis</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)      ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y-%m-%d %H:%i:%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">datetime</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">iso</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)      ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y-%m-%d %H:%i:%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">datetime</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">eur</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)      ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y-%m-%d %H.%i.%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">datetime</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">internal</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">) ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%Y%m%d%H%i%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(time,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">usa</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%h:%i:%s %p&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(time,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">jis</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%H:%i:%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(time,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">iso</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%H:%i:%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(time,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">eur</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)          ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%H.%i.%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> get_format(time,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">internal</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">)     ; </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8216;%H%i%s&#8217;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL get_format() 函数在实际中用到机会的比较少。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">6</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 拼凑日期、时间函数：makdedate(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">year</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,dayofyear), maketime(hour,minute,second)</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> makedate(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2001</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">31</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2001-01-31&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> makedate(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2001</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">32</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2001-02-01&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> maketime(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">12</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">15</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">30</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;12:15:30&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
五、MySQL 时间戳（</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">Timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">）函数</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 获得当前时间戳函数：</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">()</p>
<p>mysql</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">&gt;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">();</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">current_timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">() </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">23</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">24</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2008</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">09</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">23</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">22</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">:</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">24</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">|</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">2</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL （Unix 时间戳、日期）转换函数：</p>
<p>unix_timestamp(),<br />
unix_timestamp(date),<br />
from_unixtime(unix_timestamp),<br />
from_unixtime(unix_timestamp,format)</p>
<p>下面是示例：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> unix_timestamp(); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 1218290027</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> unix_timestamp(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 1218124800</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> unix_timestamp(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:30:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 1218169800</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> from_unixtime(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1218290027</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2008-08-09 21:53:47&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> from_unixtime(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1218124800</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2008-08-08 00:00:00&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> from_unixtime(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1218169800</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2008-08-08 12:30:00&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> from_unixtime(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1218169800</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">%Y %D %M %h:%i:%s %x</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> &#8217;2008 8th August 12:30:00 2008&#8242;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">3</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">. MySQL 时间戳（</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">）转换、增、减函数：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(date) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> date to timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(dt,time) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> dt + time</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">timestampadd(unit,interval,datetime_expr) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">timestampdiff(unit,datetime_expr1,datetime_expr2) </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;<br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
请看示例部分：</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-08 00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 08:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">01:01:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-08 09:01:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">timestamp</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 08:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">10 01:01:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-18 09:01:01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timestampadd(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 08:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-09 08:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 08:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-09 08:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL timestampadd() 函数类似于 date_add()。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timestampdiff(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">year</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2002-05-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2001-01-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> -1</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timestampdiff(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">day</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> ,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2002-05-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2001-01-01</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> -485</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timestampdiff(hour,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">,</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> -12</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-01 00:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 7</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
MySQL timestampdiff() 函数就比 </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">() 功能强多了，</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff00ff;">datediff</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">() 只能计算两个日期（date）之间相差的天数。<br />
六、MySQL 时区（timezone）转换函数</p>
<p>convert_tz(dt,from_tz,to_tz)</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> convert_tz(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">+08:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">+00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-08 04:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"><br />
时区转换也可以通过 date_add, date_sub, timestampadd 来实现。</p>
<p></span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_add(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">8</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> hour); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-08 04:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> date_sub(</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, interval </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">8</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> hour); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-08 04:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"><br />
</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #0000ff;">select</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;"> timestampadd(hour, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #808080;">-</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #800000; font-weight: bold;">8</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">, </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">2008-08-08 12:00:00</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #ff0000;">&#8216;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #000000;">); </span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;">&#8211;</span><span style="line-height: 1.5; font-size: 13px; font-family: 'Courier New'; color: #008080;"> 2008-08-08 04:00:00</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/422/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>分享 国家、省(州)、城市 数据库SQL</title>
		<link>http://www.d5s.cn/archives/418</link>
		<comments>http://www.d5s.cn/archives/418#comments</comments>
		<pubDate>Thu, 30 Dec 2010 08:23:58 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/?p=418</guid>
		<description><![CDATA[有需要的就下载吧。 谁有现成的省、市、县数据库SQL文件？ 下载地址： CountryProvinceCity.zip]]></description>
			<content:encoded><![CDATA[<p>有需要的就下载吧。<br />
谁有现成的省、市、县数据库SQL文件？</p>
<p>下载地址： <a href="http://www.d5s.cn/wp-content/uploads/2010/12/CountryProvinceCity.zip">CountryProvinceCity.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/418/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL主从服务器的一些技巧</title>
		<link>http://www.d5s.cn/archives/106</link>
		<comments>http://www.d5s.cn/archives/106#comments</comments>
		<pubDate>Fri, 14 Nov 2008 02:55:21 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/106</guid>
		<description><![CDATA[作者：老王 问题：主从服务器表类型的选择 一般的共识是主服务器使用innodb，从服务器使用myisam，以便各尽其能。 问题：主从服务器字段类型的选择 字段类型对于分页等操作有很大影响。主服务器一般是innodb，因为不涉及查询，所以可以使用varchar等来存储字符串来节省空间，从服务器一般是 myisam，因为涉及查询，所以必须在char和varchar之间仔细权衡，没有varchar, text, blob字段的表是静态表，反之是动态表，静态表的检索效率要比动态表好若干倍，一般来说，所有涉及大结果集的查询都应该尽可能保证在静态表上完成，这里 说一个例子：比如说常见的articles表有title(varchar), body(text)等字段，在做文章列表的时候，因为不是静态表，所以查询不会很快，下面开始重构解决方案：把原来的articles表拆分成 subjects表和contents表，title字段设置为一个足够的char类型放在subjects表里，body字段还保持是text类型放到 contents表里，subjects和contents表之间的关系是一对多，这样，顺带着也方便的实现了多页文章的功能，而且更重要的是在查询文章 列表的时候，操作都是在subjects静态表里完成，效率肯定会比前一种方案提升很多。 问题：主从服务器NOW()函数造成数据不一致 假设在主服务器上执行一条INSERT &#8230;. VALUES ( &#8230;, NOW())，那么在从服务器上也会同样执行一条的SQL语句，但是一来主从服务器各自的时间设置可能就不一致，二来主从服务器间的SQL同步也可能存在 时间上的的延迟，这样，NOW()在两台服务器上的结果就可能不一致。解决方法是显而易见的，就是不要使用NOW()，时间的计算在应用程序里完成。这里 介绍一个额外的小技巧：在PHP里如果想获得当前时间的时间戳，不要用time()，而应该使用$_SERVER[‘REQUEST_TIME’] （PHP版本大于5.1有效），这样少做了一次系统调用，更有效率。 问题：主从服务器读写分离时读操作失败 先重现一下问题：比如说添加一条新数据，添加成功后根据last_insert_id跳转到新添加数据的浏览页面。在此过程中添加新数据的操作是在主服务 器上完成的，浏览新数据的操作实在从服务器上完成的，不过由于主从服务器间SQL同步存在延迟，所以当使用last_insert_id在从服务器上查询 的时候，从服务器很可能还没有还没来得及同步到此记录，所以读操作失败。解决思路也不复杂，在代码里加入一个缓存层（可以使用memcached），新添 加的数据都顺手放到缓存层里一份，新数据的读操作也先查询缓存层，这样就不会再有读操作失败的问题了，当然删除或者更新数据的时候也要顺带着处理好缓存数 据，可以使用观察者模式来搞定。不过这样缓存方案只限于读取单一的记录，对于读取列表的记录的情况，则是无效的。 问题：主从服务器索引是否有必要保持一致 一般都是利用主从服务器完成读写分离，从服务器上进行读操作，主服务器进行写操作，这样的话，主服务器上仅保留主键，外键，唯一索引等必要的索引即可，以 便保持数据合法性，而对于那些原本用于优化SELECT操作的索引，可以全部删除，如此的话主服务器的写操作效率会提升很多。 转自：http://hi.baidu.com/thinkinginlamp/blog/item/5d72dd5469b1885fd0090633.html]]></description>
			<content:encoded><![CDATA[<p>作者：老王</p>
<p><strong>问题：主从服务器表类型的选择</strong></p>
<p>一般的共识是主服务器使用innodb，从服务器使用myisam，以便各尽其能。</p>
<p><strong>问题：主从服务器字段类型的选择</strong></p>
<p>字段类型对于分页等操作有很大影响。主服务器一般是innodb，因为不涉及查询，所以可以使用varchar等来存储字符串来节省空间，从服务器一般是 myisam，因为涉及查询，所以必须在char和varchar之间仔细权衡，没有varchar, text, blob字段的表是静态表，反之是动态表，静态表的检索效率要比动态表好若干倍，一般来说，所有涉及大结果集的查询都应该尽可能保证在静态表上完成，这里 说一个例子：比如说常见的articles表有title(varchar), body(text)等字段，在做文章列表的时候，因为不是静态表，所以查询不会很快，下面开始重构解决方案：把原来的articles表拆分成 subjects表和contents表，title字段设置为一个足够的char类型放在subjects表里，body字段还保持是text类型放到 contents表里，subjects和contents表之间的关系是一对多，这样，顺带着也方便的实现了多页文章的功能，而且更重要的是在查询文章 列表的时候，操作都是在subjects静态表里完成，效率肯定会比前一种方案提升很多。</p>
<p><strong>问题：</strong><strong>主从服务器</strong><strong>NOW()函数造成数据不一致</strong></p>
<p>假设在主服务器上执行一条INSERT &#8230;. VALUES ( &#8230;, NOW())，那么在从服务器上也会同样执行一条的SQL语句，但是一来主从服务器各自的时间设置可能就不一致，二来主从服务器间的SQL同步也可能存在 时间上的的延迟，这样，NOW()在两台服务器上的结果就可能不一致。解决方法是显而易见的，就是不要使用NOW()，时间的计算在应用程序里完成。这里 介绍一个额外的小技巧：在PHP里如果想获得当前时间的时间戳，不要用time()，而应该使用$_SERVER[‘REQUEST_TIME’] （PHP版本大于5.1有效），这样少做了一次系统调用，更有效率。</p>
<p><strong>问题：主从服务器读写分离时读操作失败</strong></p>
<p>先重现一下问题：比如说添加一条新数据，添加成功后根据last_insert_id跳转到新添加数据的浏览页面。在此过程中添加新数据的操作是在主服务 器上完成的，浏览新数据的操作实在从服务器上完成的，不过由于主从服务器间SQL同步存在延迟，所以当使用last_insert_id在从服务器上查询 的时候，从服务器很可能还没有还没来得及同步到此记录，所以读操作失败。解决思路也不复杂，在代码里加入一个缓存层（可以使用memcached），新添 加的数据都顺手放到缓存层里一份，新数据的读操作也先查询缓存层，这样就不会再有读操作失败的问题了，当然删除或者更新数据的时候也要顺带着处理好缓存数 据，可以使用观察者模式来搞定。不过这样缓存方案只限于读取单一的记录，对于读取列表的记录的情况，则是无效的。</p>
<p><strong>问题：主从服务器索引是否有必要保持一致</strong></p>
<p>一般都是利用主从服务器完成读写分离，从服务器上进行读操作，主服务器进行写操作，这样的话，主服务器上仅保留主键，外键，唯一索引等必要的索引即可，以 便保持数据合法性，而对于那些原本用于优化SELECT操作的索引，可以全部删除，如此的话主服务器的写操作效率会提升很多。</p>
<p>转自：<a href="http://hi.baidu.com/thinkinginlamp/blog/item/5d72dd5469b1885fd0090633.html " target="_blank">http://hi.baidu.com/thinkinginlamp/blog/item/5d72dd5469b1885fd0090633.html </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/106/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql 分组 排序 取时间最大的一条记录</title>
		<link>http://www.d5s.cn/archives/102</link>
		<comments>http://www.d5s.cn/archives/102#comments</comments>
		<pubDate>Tue, 14 Oct 2008 07:35:40 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/102</guid>
		<description><![CDATA[mysql 分组 group by, 排序 取每条记录中，时间最大的一条记录 SELECT A.* FROM test A, (SELECT aid, MAX(day) max_day FROM test GROUP BY aid) B WHERE A.aid = B.aid AND A.day = B.max_day ORDER BY a.install DESC 以下是 test 表，测试sql CREATE TABLE IF NOT &#8230; <a href="http://www.d5s.cn/archives/102">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>mysql 分组 group by, 排序 取每条记录中，时间最大的一条记录</p>
<p><span class="code"><br />
SELECT A.* FROM test A,<br />
(SELECT aid, MAX(day) max_day FROM test GROUP BY aid) B<br />
WHERE A.aid = B.aid AND A.day = B.max_day<br />
ORDER BY a.install DESC<br />
</span></p>
<p>以下是 test 表，测试sql</p>
<p><span class="code"><br />
CREATE TABLE IF NOT EXISTS `test` (<br />
`id` int(10) unsigned NOT NULL auto_increment,<br />
`install` int(10) unsigned NOT NULL,<br />
`day` int(10) unsigned NOT NULL,<br />
`aid` int(10) unsigned NOT NULL,<br />
PRIMARY KEY  (`id`)<br />
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;</span></p>
<p><span class="code"><br />
INSERT INTO `test` (`id`, `install`, `day`, `aid`) VALUES<br />
(1, 1232, 20080808, 1),<br />
(2, 2321, 20080809, 2),<br />
(3, 1236, 20080810, 3),<br />
(5, 4212, 20080809, 1),<br />
(6, 2312, 20080810, 1),<br />
(7, 1432, 20080811, 1),<br />
(8, 2421, 20080808, 2),<br />
(9, 4245, 20080811, 2),<br />
(10, 5654, 20080810, 2),<br />
(11, 412, 20080808, 3);<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/102/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DBA 数据库 db4 写入、查询测试</title>
		<link>http://www.d5s.cn/archives/100</link>
		<comments>http://www.d5s.cn/archives/100#comments</comments>
		<pubDate>Sat, 27 Sep 2008 10:32:35 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/100</guid>
		<description><![CDATA[可以先看看这里的简介 DBA数据库 （这里没有提到db4，现在应该使用db4） 安装： 打开php.ini 确定 php_dba.dll 已经载入。 之后就可以使用dba函数库了。 这个数据，就是简单的 key=&#62;value模式，和memcache差不多。 写入和查询速度都是非常快的。 如果是本机简单测试，可以使用 inifile 模式。 不过正式服务器上，一定要使用db4模式读写，因为其他模式比较慢，inifile就更慢了，还没有fopen快，所以inifile只能测试。 现在测试一下dba数据库的写入速度 每条数据是1k，写入速度分别是：  10000条       1.71057009697 100000条      21.7869038582 1000000条    765.130697012 每条数据是2k，写入速度分别是：  10000条       1.13584280014 100000条      25.066011906 1000000条     704.676019907 每条数据是3k，写入速度分别是：  10000条       0.865121126175 100000条      24.7635490894 1000000条     745.992260933 每条数据是6k，写入速度分别是：  10000条       4.17641997337 100000条      &#8230; <a href="http://www.d5s.cn/archives/100">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>可以先看看这里的简介 <a target="_blank" href="http://www.itlearner.com/code/php/group.php-9.php">DBA数据库</a> （这里没有提到db4，现在应该使用db4）</p>
<p>安装：</p>
<p>打开php.ini 确定 php_dba.dll 已经载入。</p>
<p>之后就可以使用dba函数库了。</p>
<p>这个数据，就是简单的 key=&gt;value模式，和memcache差不多。</p>
<p>写入和查询速度都是非常快的。</p>
<p>如果是本机简单测试，可以使用 inifile 模式。</p>
<p>不过正式服务器上，一定要使用db4模式读写，因为其他模式比较慢，inifile就更慢了，还没有fopen快，所以inifile只能测试。</p>
<p>现在测试一下dba数据库的写入速度</p>
<p>每条数据是1k，写入速度分别是： <br />
10000条       1.71057009697<br />
100000条      21.7869038582<br />
1000000条    765.130697012</p>
<p>每条数据是2k，写入速度分别是： <br />
10000条       1.13584280014<br />
100000条      25.066011906<br />
1000000条     704.676019907</p>
<p>每条数据是3k，写入速度分别是： <br />
10000条       0.865121126175<br />
100000条      24.7635490894<br />
1000000条     745.992260933</p>
<p>每条数据是6k，写入速度分别是： <br />
10000条       4.17641997337<br />
100000条      102.979793072<br />
1000000条     1891.55883002</p>
<p> 可以看出，如果每条数据不超过3k的情况下，写入速度还是非常快的，而且大致都在一个范围内。（当然，这个速度还和服务器性能有关系）</p>
<p> 这是对测试结果做出的压力测试：</p>
<p>读取1000000数据的bdb库（3.9G左右），每次读取10-100条数据，每条数据的键，分别是1-1000000之间</p>
<p>This is ApacheBench, Version 2.3 &lt;$Revision: 655654 $&gt;<br />
Copyright 1996 Adam Twiss, Zeus Technology Ltd, <a href="http://www.zeustech.net/">http://www.zeustech.net/</a><br />
Licensed to The Apache Software Foundation, <a href="http://www.apache.org/">http://www.apache.org/</a></p>
<p>Benchmarking si.adtest.com (be patient)<br />
Server Software:        Apache/2.2.9<br />
Server Hostname:        si.adtest.com<br />
Server Port:            80</p>
<p>Document Path:          /test_bdb/r.php<br />
Document Length:        37 bytes</p>
<p>Concurrency Level:      700<br />
Time taken for tests:   2.558 seconds<br />
Complete requests:      700<br />
Failed requests:        555<br />
   (Connect: 0, Receive: 0, Length: 555, Exceptions: 0)<br />
Write errors:           0<br />
Total transferred:      193705 bytes<br />
HTML transferred:       26405 bytes<br />
Requests per second:    273.62 [#/sec] (mean)<br />
Time per request:       2558.262 [ms] (mean)<br />
Time per request:       3.655 [ms] (mean, across all concurrent requests)<br />
Transfer rate:          73.94 [Kbytes/sec] received</p>
<p>Connection Times (ms)<br />
              min  mean[+/-sd] median   max<br />
Connect:        0    2   1.4      2       7<br />
Processing:    82  471 118.6    517     559<br />
Waiting:       26  443 118.9    484     544<br />
Total:         82  473 119.2    519     559</p>
<p>Percentage of the requests served within a certain time (ms)<br />
  50%    519<br />
  66%    524<br />
  75%    528<br />
  80%    533<br />
  90%    539<br />
  95%    542<br />
  98%    547<br />
  99%    552<br />
 100%    559 (longest request)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/100/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>利用Tokyo Tyrant构建兼容Memcached协议、支持故障转移、高并发的分布式key-value持久存储系统</title>
		<link>http://www.d5s.cn/archives/97</link>
		<comments>http://www.d5s.cn/archives/97#comments</comments>
		<pubDate>Sat, 09 Aug 2008 13:09:40 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/97</guid>
		<description><![CDATA[转自：http://blog.s135.com/read.php/362.htm 这么牛X的数据库，为什么没人广泛采用呢？ 估计 1、大部分人 不了解这东西； 2、项目管理层的人也许了解，但不敢尝试。 希望能有机会用到这样的好东西。 Tokyo Cabinet 是日本人 平林幹雄 开发的一款 DBM 数据库，该数据库读写非常快，哈希模式写入100万条数据只需0.643秒，读取100万条数据只需0.773秒，是 Berkeley DB 等 DBM 的几倍。 Tokyo Tyrant 是由同一作者开发的 Tokyo Cabinet 数据库网络接口。它拥有Memcached兼容协议，也可以通过HTTP协议进行数据交换。 Tokyo Tyrant 加上 Tokyo Cabinet，构成了一款支持高并发的分布式持久存储系统，对任何原有Memcached客户端来讲，可以将Tokyo Tyrant看成是一个Memcached，但是，它的数据是可以持久存储的。这一点，跟新浪的Memcachedb性质一样。 相比Memcachedb而言，Tokyo Tyrant具有以下优势： 1、故障转移：Tokyo Tyrant支持双机互为主辅模式，主辅库均可读写，而Memcachedb目前支持类似MySQL主辅库同步的方式实现读写分离，支持“主服务器可读写、辅助服务器只读”模式。 这里使用 $memcache-&#62;addServer 而不是 $memcache-&#62;connect 去连接 &#8230; <a href="http://www.d5s.cn/archives/97">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>转自：<a href="http://blog.s135.com/read.php/362.htm " target="_blank">http://blog.s135.com/read.php/362.htm </a></p>
<p>这么牛X的数据库，为什么没人广泛采用呢？</p>
<p>估计</p>
<p>1、大部分人 不了解这东西；</p>
<p>2、项目管理层的人也许了解，但不敢尝试。</p>
<p>希望能有机会用到这样的好东西。</p>
<p><a href="http://tokyocabinet.sourceforge.net/" target="_blank">Tokyo Cabinet</a> 是日本人 平林幹雄 开发的一款 DBM 数据库，该数据库读写非常快，哈希模式写入100万条数据只需0.643秒，读取100万条数据只需0.773秒，是 Berkeley DB 等 DBM 的几倍。</p>
<p><a href="http://blog.s135.com/attachment/200808/tokyocabinet.gif" target="_blank"><img src="http://blog.s135.com/attachment/200808/tokyocabinet.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" /></a></p>
<hr />
<a href="http://tokyocabinet.sourceforge.net/tyrantdoc/" target="_blank">Tokyo Tyrant</a> 是由同一作者开发的 Tokyo Cabinet 数据库网络接口。它拥有Memcached兼容协议，也可以通过HTTP协议进行数据交换。</p>
<p>Tokyo Tyrant 加上 Tokyo Cabinet，构成了一款支持高并发的分布式持久存储系统，对任何原有Memcached客户端来讲，可以将Tokyo Tyrant看成是一个Memcached，但是，它的数据是可以持久存储的。这一点，跟新浪的<a href="http://memcachedb.org/" target="_blank">Memcachedb</a>性质一样。</p>
<p><strong>相比Memcachedb而言，Tokyo Tyrant具有以下优势：</strong></p>
<p>1、故障转移：Tokyo Tyrant支持双机互为主辅模式，主辅库均可读写，而Memcachedb目前支持类似MySQL主辅库同步的方式实现读写分离，支持“主服务器可读写、辅助服务器只读”模式。</p>
<p><a href="http://blog.s135.com/attachment/200808/addserver.gif" target="_blank"><img src="http://blog.s135.com/attachment/200808/addserver.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" /></a></p>
<p>这里使用 $memcache-&gt;addServer 而不是 $memcache-&gt;connect 去连接 Tokyo Tyrant 服务器，是因为当 Memcache 客户端使用 addServer 服务器池时，是根据“crc32(key) % current_server_num”哈希算法将 key 哈希到不同的服务器的，PHP、C 和 python 的客户端都是如此的算法。Memcache 客户端的 addserver 具有故障转移机制，当 addserver 了2台 Memcached 服务器，而其中1台宕机了，那么 current_server_num 会由原先的2变成1。</p>
<p>引用 memcached 官方网站和 PHP 手册中的两段话：</p>
<p class="quote">
<p class="quote-title">引用</p>
<p class="quote-content">http://www.danga.com/memcached/<br />
If a host goes down, the API re-maps that dead host&#8217;s requests onto the servers that are available.</p>
<p><a href="http://cn.php.net/manual/zh/function.Memcache-addServer.php" target="_blank">http://cn.php.net/manual/zh/function.Memcache-addServer.php</a><br />
Failover may occur at any stage in any of the methods, as long as other servers are available the request the user won&#8217;t notice. Any kind of socket or Memcached server level errors (except out-of-memory) may trigger the failover. Normal client errors such as adding an existing key will not trigger a failover.</p>
<hr />
2、日志文件体积小：Tokyo Tyrant用于主辅同步的日志文件比较小，大约是数据库文件的1.3倍，而Memcachedb的同步日志文件非常大，如果不定期清理，很容易将磁盘写满。</p>
<hr />
3、超大数据量下表现出色：</p>
<p><a href="http://blog.s135.com/attachment/200808/tokyotyrant.gif" target="_blank"><img src="http://blog.s135.com/attachment/200808/tokyotyrant.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" /></a></p>
<p>但是，Tokyo Tyrant 也有缺点：<span style="color: #ff0000">在32位操作系统下，作为 Tokyo Tyrant 后端存储的 Tokyo Cabinet 数据库单个文件不能超过2G，而64位操作系统则不受这一限制。</span>所以，如果使用 Tokyo Tyrant，推荐在64位CPU、操作系统上安装运行。</p>
<hr />
<strong>一、安装</strong><br />
1、首先编译安装tokyocabinet数据库</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">wget <a href="http://blog.s135.com/soft/linux/memcached/tokyocabinet-1.3.1.tar.gz" target="_blank">http://blog.s135.com/soft/linux/memcached/tokyocabinet-1.3.1.tar.gz</a><br />
tar zxvf tokyocabinet-1.3.1.tar.gz<br />
cd tokyocabinet-1.3.1/<br />
./configure<br />
make<br />
make install<br />
cd ../</p>
<p>2、然后编译安装tokyotyrant</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">wget <a href="http://blog.s135.com/soft/linux/memcached/tokyotyrant-1.0.0.tar.gz" target="_blank">http://blog.s135.com/soft/linux/memcached/tokyotyrant-1.0.0.tar.gz</a><br />
tar zxvf tokyotyrant-1.0.0.tar.gz<br />
cd tokyotyrant-1.0.0/<br />
./configure<br />
make<br />
make install<br />
cd ../</p>
<hr />
<strong>二、配置</strong><br />
<strong>1、创建tokyotyrant数据文件存放目录</strong></p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">mkdir -p /ttserver/</p>
<p><strong>2、启动tokyotyrant的主进程（ttserver）</strong><br />
<strong>(1)、单机模式</strong></p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">ulimit -SHn 51200<br />
ttserver -host 127.0.0.1 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 1 -rts /ttserver/ttserver.rts /ttserver/database.tch</p>
<p><strong>(2)、双机互为主辅模式</strong><br />
服务器192.168.1.91：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">ulimit -SHn 51200<br />
ttserver -host 192.168.1.91 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 91 -mhost 192.168.1.92 -mport 11211 -rts /ttserver/ttserver.rts /ttserver/database.tch</p>
<p>服务器192.168.1.92：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">ulimit -SHn 51200<br />
ttserver -host 192.168.1.92 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 92 -mhost 192.168.1.91 -mport 11211 -rts /ttserver/ttserver.rts /ttserver/database.tch</p>
<p><strong>(3)、参数说明</strong><br />
ttserver [-host name] [-port num] [-thnum num] [-tout num] [-dmn] [-pid path] [-log path] [-ld|-le] [-ulog path] [-ulim num] [-uas] [-sid num] [-mhost name] [-mport num] [-rts path] [dbname]</p>
<p>-host name : 指定需要绑定的服务器域名或IP地址。默认绑定这台服务器上的所有IP地址。<br />
-port num : 指定需要绑定的端口号。默认端口号为1978<br />
-thnum num : 指定线程数。默认为8个线程。<br />
-tout num : 指定每个会话的超时时间（单位为秒）。默认永不超时。<br />
-dmn : 以守护进程方式运行。<br />
-pid path : 输出进程ID到指定文件（这里指定文件名）。<br />
-log path : 输出日志信息到指定文件（这里指定文件名）。<br />
-ld : 在日志文件中还记录DEBUG调试信息。<br />
-le : 在日志文件中仅记录错误信息。<br />
-ulog path : 指定同步日志文件存放路径（这里指定目录名）。<br />
-ulim num : 指定每个同步日志文件的大小（例如128m）。<br />
-uas : 使用异步IO记录更新日志（使用此项会减少磁盘IO消耗，但是数据会先放在内存中，不会立即写入磁盘，如果重启服务器或ttserver进程被kill掉，将导致部分数据丢失。一般情况下不建议使用）。<br />
-sid num : 指定服务器ID号（当使用主辅模式时，每台ttserver需要不同的ID号）<br />
-mhost name : 指定主辅同步模式下，主服务器的域名或IP地址。<br />
-mport num : 指定主辅同步模式下，主服务器的端口号。<br />
-rts path : 指定用来存放同步时间戳的文件名。</p>
<p>如果使用的是哈希数据库，可以指定参数“#bnum=xxx”来提高性能。它可以指定bucket存储桶的数量。例如指定“#bnum=1000000”，就可以将最新最热的100万条记录缓存在内存中：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">ttserver -host 127.0.0.1 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 1 -rts /ttserver/ttserver.rts /ttserver/database.tch#bnum=1000000</p>
<p>如果大量的客户端访问ttserver，请确保文件描述符够用。许多服务器的默认文件描述符为1024，可以在启动ttserver前使用ulimit命令提高这项值。例如：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">ulimit -SHn 51200</p>
<p><strong>3、停止tokyotyrant（ttserver）</strong></p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">ps -ef | grep ttserver</p>
<p>找到ttserver的进程号并kill，例如：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">kill -TERM 2159</p>
<hr />
<strong>三、调用</strong><br />
<strong>1、任何Memcached客户端均可直接调用tokyotyrant。</strong></p>
<p><strong>2、还可以通过HTTP方式调用，下面以Linux的curl命令为例，介绍如何操作tokyotyrant：</strong><br />
(1)、写数据，将数据“value”写入到“key”中：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">curl -X PUT <a href="http://127.0.0.1:11211/key" target="_blank">http://127.0.0.1:11211/key</a> -d “value”</p>
<p>(2)、读数据，读取“key”中数据：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">curl <a href="http://127.0.0.1:11211/key" target="_blank">http://127.0.0.1:11211/key</a></p>
<p>(3)、删数据，删除“key”：</p>
<p style="border: 1px dashed #00a0c6; margin: 5px 5px 0px; padding: 3px; background-color: #ffffff">curl -X DELETE <a href="http://127.0.0.1:11211/key" target="_blank">http://127.0.0.1:11211/key</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/97/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MYSQL 主从服务器配置</title>
		<link>http://www.d5s.cn/archives/95</link>
		<comments>http://www.d5s.cn/archives/95#comments</comments>
		<pubDate>Thu, 10 Jul 2008 05:12:59 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/95</guid>
		<description><![CDATA[mysql服务器的主从配置，本来是一件很简单的事情，无奈不是从零开始，总是在别人已经安装好的mysql服务器之上 ，这就会牵扯到，mysql的版本，启动文件，等一些问题。 不过没关系，先问清楚两点 1、mysql配置文件my.cnf的位置 2、如何启动、停止mysql，找好启动文件 假设有两台机器，已经安装好了mysql（尽量同版本，且两台机器同一网络，可以ping通） 有朋友说：“从服务器，不能低于主服务器的版本”，不过我是低于的，没有出现问题。 主机A: 192.168.1.100 从机B:192.168.1.101 可以有多台从机 1、先登录主机 A mysql&#62;GRANT REPLICATION SLAVE ON *.* TO ‘backup’@&#8217;192.168.1.101&#8216; IDENTIFIED BY ‘123456’; 赋予从机权限，有多台丛机，就执行多次 2、 打开主机A的my.cnf，输入 server-id               = 1    #主机标示，整数 log_bin                 = /var/log/mysql/mysql-bin.log   #确保此文件可写 read-only       =0  #主机，读写都可以 binlog-do-db    =test   #需要备份数据，多个写多行 &#8230; <a href="http://www.d5s.cn/archives/95">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>mysql服务器的主从配置，本来是一件很简单的事情，无奈不是从零开始，总是在别人已经安装好的mysql服务器之上 ，这就会牵扯到，mysql的版本，启动文件，等一些问题。</p>
<p>不过没关系，先问清楚两点</p>
<p>1、mysql配置文件my.cnf的位置</p>
<p>2、如何启动、停止mysql，找好启动文件</p>
<p>假设有两台机器，已经安装好了mysql（尽量同版本，且两台机器同一网络，可以ping通）</p>
<p>有朋友说：“从服务器，不能低于主服务器的版本”，不过我是低于的，没有出现问题。</p>
<p>主机A: 192.168.1.100</p>
<p>从机B:192.168.1.101</p>
<p>可以有多台从机</p>
<p>1、先登录主机 A</p>
<p>mysql&gt;<span style="font-size: 12pt; line-height: 150%">GRANT </span>REPLICATION SLAVE<span style="font-size: 12pt; line-height: 150%"> ON *.* TO ‘backup’@&#8217;</span>192.168.1.101<span style="font-size: 12pt; line-height: 150%">&#8216; IDENTIFIED BY ‘123456’;</span></p>
<p>赋予从机权限，有多台丛机，就执行多次</p>
<p>2、 打开主机A的my.cnf，输入</p>
<p>server-id               = 1    #主机标示，整数<br />
log_bin                 = /var/log/mysql/mysql-bin.log   #确保此文件可写<br />
read-only       =0  #主机，读写都可以<br />
binlog-do-db    =test   #需要备份数据，多个写多行<br />
binlog-ignore-db=mysql #不需要备份的数据库，多个写多行</p>
<p>3、打开从机B的my.cnf，输入</p>
<p>server-id               = 2<br />
log_bin                 = /var/log/mysql/mysql-bin.log<br />
master-host     =192.168.1.100<br />
master-user     =<span style="font-size: 12pt; line-height: 150%">backup</span><br />
master-pass     =<span style="font-size: 12pt; line-height: 150%">123456</span><br />
master-port     =3306<br />
master-connect-retry=60 #如果从服务器发现主服务器断掉，重新连接的时间差(秒)<br />
replicate-do-db =test #只复制某个库<br />
replicate-ignore-db=mysql #不复制某个库</p>
<p>4、同步数据库</p>
<p>有多种方法，我说最笨的一种，先mysqldump导出主机A的数据test为 test.sql</p>
<p>然后在，从机B上建立数据库test，mysql导入 test.sql到test库中</p>
<p>5、先重启主机A的mysql，再重启从机B的mysql</p>
<p>6、验证</p>
<p>在主机A中，mysql&gt;show master status\G;</p>
<p>在从机B中，mysql&gt;show slave status\G;</p>
<p>能看到大致这些内容</p>
<p>File: mysql-bin.000001<br />
Position: 1374<br />
Binlog_Do_DB: test<br />
Binlog_Ignore_DB: mysql</p>
<p>可以在主机A中，做一些INSERT, UPDATE, DELETE 操作，看看主机B中，是否已经被修改<br />
以下是一些其他朋友写的，我也做了参考</p>
<p>http://www.ningoo.net/html/2007/mysql_replication_configuration.html</p>
<p>http://leftleg.hzpub.com/post/645/</p>
<p>http://blog.zhangjianfeng.com/article/705</p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/95/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MYSQL 事务 笔记</title>
		<link>http://www.d5s.cn/archives/93</link>
		<comments>http://www.d5s.cn/archives/93#comments</comments>
		<pubDate>Fri, 27 Jun 2008 16:29:41 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/93</guid>
		<description><![CDATA[ 由于项目设计里面，牵扯到了金钱的转移，于是就要用到MYSQL的事务处理，来保证一组处理结果的正确性 用了事务，就不可避免的要牺牲一部分速度，来保证数据的正确性。 只有InnoDB支持事务 事务 ACID Atomicity（原子性）、Consistency（稳定性）、Isolation（隔离性）、Durability（可靠性） 1、事务的原子性 一组事务，要么成功；要么撤回。 2、稳定性 有非法数据（外键约束之类），事务撤回。 3、隔离性 事务独立运行。 一个事务处理后的结果，影响了其他事务，那么其他事务会撤回。 事务的100%隔离，需要牺牲速度。 4、可靠性 软、硬件崩溃后，InnoDB数据表驱动会利用日志文件重构修改。 可靠性和高速度不可兼得， innodb_flush_log_at_trx_commit选项 决定什么时候吧事务保存到日志里。 开启事务  START TRANSACTION 或 BEGIN 提交事务（关闭事务）  COMMIT 放弃事务（关闭事务）  ROLLBACK 折返点  SAVEPOINT adqoo_1  ROLLBACK TO SAVEPOINT adqoo_1  发生在折返点 adqoo_1 之前的事务被提交，之后的被忽略 事务的终止 设置“自动提交”模式 &#8230; <a href="http://www.d5s.cn/archives/93">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong> </strong>由于项目设计里面，牵扯到了金钱的转移，于是就要用到MYSQL的事务处理，来保证一组处理结果的正确性</p>
<p>用了事务，就不可避免的要牺牲一部分速度，来保证数据的正确性。</p>
<p><strong>只有InnoDB支持事务</strong></p>
<p>事务 ACID Atomicity（原子性）、Consistency（稳定性）、Isolation（隔离性）、Durability（可靠性）</p>
<p>1、事务的原子性<br />
一组事务，要么成功；要么撤回。</p>
<p>2、稳定性<br />
有非法数据（外键约束之类），事务撤回。</p>
<p>3、隔离性<br />
事务独立运行。<br />
一个事务处理后的结果，影响了其他事务，那么其他事务会撤回。<br />
事务的100%隔离，需要牺牲速度。</p>
<p>4、可靠性<br />
软、硬件崩溃后，InnoDB数据表驱动会利用日志文件重构修改。<br />
可靠性和高速度不可兼得， innodb_flush_log_at_trx_commit选项 决定什么时候吧事务保存到日志里。<br />
<strong>开启事务<br />
</strong> START TRANSACTION 或 BEGIN</p>
<p><strong>提交事务</strong>（关闭事务）<br />
 COMMIT</p>
<p><strong>放弃事务</strong>（关闭事务）<br />
 ROLLBACK</p>
<p><strong>折返点<br />
</strong> SAVEPOINT adqoo_1<br />
 ROLLBACK TO SAVEPOINT adqoo_1<br />
 发生在折返点 adqoo_1 之前的事务被提交，之后的被忽略</p>
<p><strong>事务的终止</strong></p>
<p>设置“自动提交”模式<br />
 SET AUTOCOMMIT = 0<br />
 每条SQL都是同一个事务的不同命令，之间由 COMMIT 或 ROLLBACK隔开<br />
 掉线后，没有 COMMIT 的事务都被放弃</p>
<p><strong>事务锁定模式</strong></p>
<p>系统默认： 不需要等待某事务结束，可直接查询到结果，但不能再进行修改、删除。<br />
缺点：查询到的结果，可能是已经过期的。<br />
优点：不需要等待某事务结束，可直接查询到结果。</p>
<p>需要用以下模式来设定锁定模式</p>
<p>1、SELECT …… LOCK IN SHARE MODE（共享锁）<br />
查询到的数据，就是数据库在这一时刻的数据（其他已commit事务的结果，已经反应到这里了）<br />
SELECT 必须等待，某个事务结束后才能执行</p>
<p>2、SELECT …… FOR UPDATE（排它锁）<br />
例如 SELECT * FROM tablename WHERE id&lt;200<br />
那么id&lt;200的数据，被查询到的数据，都将不能再进行修改、删除、SELECT …… LOCK IN SHARE MODE操作<br />
一直到此事务结束</p>
<p>共享锁 和 排它锁 的区别：在于是否阻断其他客户发出的 SELECT …… LOCK IN SHARE MODE命令</p>
<p>3、INSERT / UPDATE / DELETE<br />
所有关联数据都会被锁定，加上排它锁</p>
<p>4、防插入锁<br />
例如 SELECT * FROM tablename WHERE id&gt;200<br />
那么id&gt;200的记录无法被插入</p>
<p>5、死锁<br />
自动识别死锁<br />
先进来的进程被执行，后来的进程收到出错消息，并按ROLLBACK方式回滚<br />
innodb_lock_wait_timeout = n 来设置最长等待时间，默认是50秒</p>
<p><strong>事务隔离模式</strong></p>
<p>SET [SESSION|GLOBAL] TRANSACTION ISOLATION LEVEL<br />
READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE<br />
1、不带SESSION、GLOBAL的SET命令<br />
 只对下一个事务有效<br />
2、SET SESSION<br />
 为当前会话设置隔离模式<br />
3、SET GLOBAL<br />
 为以后新建的所有MYSQL连接设置隔离模式（当前连接不包括在内）</p>
<p><strong>隔离模式</strong></p>
<p>   READ UNCOMMITTED<br />
 不隔离SELECT<br />
 其他事务未完成的修改（未COMMIT），其结果也考虑在内</p>
<p>   READ COMMITTED<br />
 把其他事务的 COMMIT 修改考虑在内<br />
 同一个事务中，同一 SELECT 可能返回不同结果</p>
<p>   REPEATABLE READ（默认）<br />
 不把其他事务的修改考虑在内，无论其他事务是否用COMMIT命令提交过<br />
 同一个事务中，同一 SELECT 返回同一结果（前提是本事务，不修改）</p>
<p>   SERIALIZABLE<br />
 和REPEATABLE READ类似，给所有的SELECT都加上了 共享锁</p>
<p>出错处理<br />
 根据出错信息，执行相应的处理</p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/93/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL DBA 管理 常用 命令</title>
		<link>http://www.d5s.cn/archives/92</link>
		<comments>http://www.d5s.cn/archives/92#comments</comments>
		<pubDate>Wed, 25 Jun 2008 06:33:47 +0000</pubDate>
		<dc:creator>owen</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.d5s.cn/archives/92</guid>
		<description><![CDATA[虽然自己不是DBA，但是作为一个程序员，多多少少，应该了解一些数据库方面的东西，并不能只关心程序，不考虑数据库，看到一篇文章，就先转过来，也许以后自己哪天会用到。 查看mysql的某个选项 show variables like &#8216;%VAR_NAME%&#8217;; select @@VAR_NAME;     在Linux下管理MySQL数据库的时候总有一些很紧急的情况，发现数据库突然变得压力很大了，那么作为一个DBA，也许需要一些常用的手段或者说命令去分析问题出现在哪里，然后解决： 数据库突然产生压力时查看正在查询的SQL:(如果这里内容太多表示并发执行的SQL过多，或许数据库堵塞了，会越来越慢，正常情况下这里应该很少有东西的，也就是连接都在Sleep状态） /usr/local/mysql/bin/mysql -uroot -ppassword databaseName  -e “show full processlist” &#124; grep -v Sleep 正在运行的SQL太多了，看不过来，那需要排序了，看持续执行时间最长的那些SQL： /usr/local/mysql/bin/mysql -uroot -ppassword databaseName  -e “show full processlist” &#124; grep -v Sleep &#124; sort -k6rn &#62;sort.tmp &#8230; <a href="http://www.d5s.cn/archives/92">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>虽然自己不是DBA，但是作为一个程序员，多多少少，应该了解一些数据库方面的东西，并不能只关心程序，不考虑数据库，看到一篇文章，就先转过来，也许以后自己哪天会用到。</p>
<p>查看mysql的某个选项<br />
<span class="code">show variables like &#8216;%VAR_NAME%&#8217;;<br />
select @@VAR_NAME;<br />
</span></p>
<p>    在Linux下管理MySQL数据库的时候总有一些很紧急的情况，发现数据库突然变得压力很大了，那么作为一个DBA，也许需要一些常用的手段或者说命令去分析问题出现在哪里，然后解决：</p>
<p>数据库突然产生压力时查看正在查询的SQL:(如果这里内容太多表示并发执行的SQL过多，或许数据库堵塞了，会越来越慢，正常情况下这里应该很少有东西的，也就是连接都在Sleep状态）<br />
/usr/local/mysql/bin/mysql -uroot -ppassword databaseName  -e “show full processlist” | grep -v Sleep</p>
<p>正在运行的SQL太多了，看不过来，那需要排序了，看持续执行时间最长的那些SQL：<br />
/usr/local/mysql/bin/mysql -uroot -ppassword databaseName  -e “show full processlist” | grep -v Sleep | sort -k6rn &gt;sort.tmp</p>
<p>如果发现IOWait很高，请查看临时表的生成情况，特别是disk tmp table:<br />
/usr/local/mysql/bin/mysql -uroot -ppassword databaseName  -e “show global status like &#8216;%tmp%&#8217;”</p>
<p>通过这样一些办法可以查看数据库都在忙什么，那些忙的SQL又具体在哪一个步骤上卡住了，是在创建磁盘临时文件、Sending Data、statistics？依照不同的原因来解决问题</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
关于Mysql Replication日常管理，重做，问题分析时常用的办法：</p>
<p>重做Slave，或者Master变化等等，需要将Slave与新的Master同步：<br />
change master to master_host=IP,master_user=&#8217;replication userName&#8217;,master<br />
_password=&#8217;replication Passwrod&#8217;,master_log_file=&#8217;log-bin.000001&#8242;,master_log_pos=0;</p>
<p>导出数据成SQL文本，慎用，根据你的DB大小会锁表，导致堵塞其他访问：<br />
nohup /usr/local/mysql/bin/mysqldump &#8211;database DATABASEName -uUserName -pPassWord &#8211;lock-all-tables -F &gt;DATA20070519.sql &amp;<br />
-F后会刷新Master Log这样配合上面的Change Master可以让Slave进行同步</p>
<p>只导出数据库的结构（没有任何内容）<br />
/usr/local/mysql/bin/mysqldump -d DATABASEName -uUserName -pPassWord &gt;DATA20070519.structure</p>
<p>只导出数据库的数据（没有创建表结构的语句等等）<br />
/usr/local/mysql/bin/mysqldump -t DATABASEName -uUserName -pPassWord &gt;DATA20070519.data</p>
<p>同步的时候出现问题（或者其他问题）了，根据同步出现问题的位置（偏移量），查看Binlog的具体内容<br />
/usr/local/mysql/bin/mysqlbinlog binlogFileName &#8211;start-position=偏移量<br />
呵呵，我们碰到过Master执行的SQL到了Slave会报语法错误，够诡异吧！不过就是这样查到了原因：如果通过存储过程将bit的内容改为1就会出现这样的问题，后来将bit改为tinyint(1)就好了</p>
<p>授权给某一台Slave拥有复制的权限：<br />
grant replication slave on *.* to 用户名@IP identified by &#8216;密码&#8217;;</p>
<p>查看Slave状态：<br />
Show slave status \G<br />
查看Master状态：<br />
Show master status;<br />
重置Slave（慎用）<br />
reset slave;<br />
Slave出现问题了，先跳过这一条语句（请确认所要跳过的具体内容不会影响后面的同步，确认方法查看Binlog文件）：<br />
set global sql_slave_skip_counter=1; (记得先暂停Slave：stop slave; 然后重启Slave：start slave;)</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
纯粹Linux相关的：<br />
tcpdump -A “dst port 3306&#8243; 查看3306端口的通信具体内容 <img src='http://www.d5s.cn/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>转自：<a target="_blank" href="http://mrtips.blog.hexun.com/5904126_d.html">http://mrtips.blog.hexun.com/5904126_d.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.d5s.cn/archives/92/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

