博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
25.5. Date and Time
阅读量:6693 次
发布时间:2019-06-25

本文共 5377 字,大约阅读时间需要 17 分钟。

SELECT NOW(),CURRENT_TIMESTAMP(),SYSDATE();

25.5.1. year/month/day hour:minite:second

mysql> select year('2012-03-20');+--------------------+| year('2012-03-20') |+--------------------+|               2012 |+--------------------+1 row in set (0.00 sec)mysql> select month('2012-03-20');+---------------------+| month('2012-03-20') |+---------------------+|                   3 |+---------------------+1 row in set (0.00 sec)mysql> select day('2012-03-20');+-------------------+| day('2012-03-20') |+-------------------+|                20 |+-------------------+1 row in set (0.00 sec)mysql> select hour('12:30:55');+------------------+| hour('12:30:55') |+------------------+|               12 |+------------------+1 row in set (0.00 sec)mysql> select minute('12:30:55');+--------------------+| minute('12:30:55') |+--------------------+|                 30 |+--------------------+1 row in set (0.00 sec)mysql> select second('12:30:55');+--------------------+| second('12:30:55') |+--------------------+|                 55 |+--------------------+1 row in set (0.00 sec)

25.5.2. Unix time

mysql> SELECT UNIX_TIMESTAMP('2005-03-27 02:00:00');+---------------------------------------+| UNIX_TIMESTAMP('2005-03-27 02:00:00') |+---------------------------------------+|                            1111885200 |+---------------------------------------+mysql> SELECT FROM_UNIXTIME(1111885200);+---------------------------+| FROM_UNIXTIME(1111885200) |+---------------------------+| 2005-03-27 03:00:00       |+---------------------------+
SELECT UNIX_TIMESTAMP('2012-01-01 00:00:00');SELECT UNIX_TIMESTAMP('2012-07-30 00:00:00');SELECT UNIX_TIMESTAMP();SELECT UNIX_TIMESTAMP('2009-08-06') ;SELECT UNIX_TIMESTAMP( curdate( ) );select FROM_UNIXTIME(UNIX_TIMESTAMP('2012-07-30 00:00:00'), '%Y-%m-%d');SELECT FROM_UNIXTIME( 1249488000, '%Y年%m月%d日' );select FROM_UNIXTIME(createtime, '%m') as month, count(1) as count from members where createtime BETWEEN UNIX_TIMESTAMP('2012-01-01 00:00:00') and UNIX_TIMESTAMP('2012-12-31 00:00:00') group by FROM_UNIXTIME(createtime, '%m');select FROM_UNIXTIME(createtime, '%m') as month, count(1) as count from members where createtime BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') and UNIX_TIMESTAMP('2011-12-31 00:00:00') group by FROM_UNIXTIME(createtime, '%m');select FROM_UNIXTIME(createtime, '%m-%d') as month, count(1) as count from members where createtime BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') and UNIX_TIMESTAMP('2011-12-31 00:00:00') group by FROM_UNIXTIME(createtime, '%m-%d');select FROM_UNIXTIME(createtime, '%m-%d') as month, count(1) as count from members where createtime BETWEEN UNIX_TIMESTAMP('2012-01-01 00:00:00') and UNIX_TIMESTAMP('2012-12-31 00:00:00') group by FROM_UNIXTIME(createtime, '%m-%d');

25.5.3. DATE_FORMAT

DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。

语法DATE_FORMAT(date,format)date 参数是合法的日期。format 规定日期/时间的输出格式。可以使用的格式有:格式	描述%a	缩写星期名%b	缩写月名%c	月,数值%D	带有英文前缀的月中的天%d	月的天,数值(00-31)%e	月的天,数值(0-31)%f	微秒%H	小时 (00-23)%h	小时 (01-12)%I	小时 (01-12)%i	分钟,数值(00-59)%j	年的天 (001-366)%k	小时 (0-23)%l	小时 (1-12)%M	月名%m	月,数值(00-12)%p	AM 或 PM%r	时间,12-小时(hh:mm:ss AM 或 PM)%S	秒(00-59)%s	秒(00-59)%T	时间, 24-小时 (hh:mm:ss)%U	周 (00-53) 星期日是一周的第一天%u	周 (00-53) 星期一是一周的第一天%V	周 (01-53) 星期日是一周的第一天,与 %X 使用%v	周 (01-53) 星期一是一周的第一天,与 %x 使用%W	星期名%w	周的天 (0=星期日, 6=星期六)%X	年,其中的星期日是周的第一天,4 位,与 %V 使用%x	年,其中的星期一是周的第一天,4 位,与 %v 使用%Y	年,4 位%y	年,2 位

实例

下面的脚本使用 DATE_FORMAT() 函数来显示不同的格式。我们使用 NOW() 来获得当前的日期/时间:DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')DATE_FORMAT(NOW(),'%m-%d-%Y')DATE_FORMAT(NOW(),'%d %b %y')DATE_FORMAT(NOW(),'%d %b %Y %T:%f')SELECT DATE_FORMAT(NOW(),'%Y-%m-%d');select DATE_FORMAT(asctime,'%Y-%m-%d') as Date, count(1) as Count from logging where tag='www' and facility='login' group by DATE_FORMAT(asctime,'%Y-%m-%d') order by asctime desc;

25.5.4. DATE_SUB/DATE_ADD

当前时间向后推10天

mysql> select DATE_SUB(now(), INTERVAL 240 HOUR);+------------------------------------+| DATE_SUB(now(), INTERVAL 240 HOUR) |+------------------------------------+| 2012-03-09 10:26:03                |+------------------------------------+1 row in set (0.00 sec)mysql> select DATE_SUB(now(), INTERVAL 24 HOUR);+-----------------------------------+| DATE_SUB(now(), INTERVAL 24 HOUR) |+-----------------------------------+| 2012-03-18 10:28:43               |+-----------------------------------+1 row in set (0.00 sec)
DELETE from Message where created < DATE_sub(now(), INTERVAL 240 HOUR);select * from PRICES_HISTORY where DATE_FORMAT(TIME ,GET_FORMAT(DATE,'ISO')) = (select if ( WEEKDAY(CURRENT_DATE())=6 , DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY) , CURRENT_DATE()))

25.5.4.1. DATE_ADD

SELECT DATE_ADD('1998-01-02', INTERVAL 31 DAY);

25.5.5. datediff / timediff

计算时间差,两个时间相减结果

mysql> select timediff('22:20:00','17:30:00');+---------------------------------+| timediff('22:20:00','17:30:00') |+---------------------------------+| 04:50:00                        |+---------------------------------+1 row in set (0.00 sec)mysql> select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00');+--------------------------------------------------------+| datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00') |+--------------------------------------------------------+|                                                      7 |+--------------------------------------------------------+1 row in set (0.00 sec)

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
Git 如何拆除核弹起爆码,以及 topgit 0.7到0.8的变迁
查看>>
zencart网站怎么隐藏 51la ,cnzz的统计代码图标
查看>>
MacOS X,安装和配置Maven
查看>>
phonegap支持打电话
查看>>
LAMP之apache安装
查看>>
Linux 部署websphere
查看>>
Nginx自动跳转https
查看>>
chapter1 Accessing the command line using the local console
查看>>
“can’t create transaction lock on /var/lib/rpm/__db.000”错误解决
查看>>
siege 测试
查看>>
微博一键分享主要通过对指定 URL 添加各种参数来实现
查看>>
panabit+syslog打造中小型企业流量监控系统
查看>>
苏州朋友公司 windows server 2008 打印机管理
查看>>
我的友情链接
查看>>
aotufs
查看>>
Mysql服务启动1067错误
查看>>
HTML小结
查看>>
django表单模块学习
查看>>
<ps> ps aux 和ps -aux和 ps -ef的区别和选择
查看>>
Microsoft Office Web Apps Server2013 For Lync2013 PPT部署安装
查看>>