mysql中关于字段变量的计算
计算tbl_test 表中的两个字段一个是发布时间一个是创建时间,使用变量TIMESTAMPDIFF 来计算二者的差值,
creattime - pubdate。minute的意思是以分钟来做结果单位,limit 2 是两条记录
select TIMESTAMPDIFF(minute, pubdate, createtime)from tbl_test limit 2;
下面是字段的值和计算结果
+---------------------+---------------------+
| pubdate | createtime |
+---------------------+---------------------+
| 2015-06-17 04:59:28 | 2015-06-17 04:59:13 |
| 2015-06-18 13:21:01 | 2015-06-17 10:57:24 |
+---------------------+---------------------+
--------------------------------------------+
| TIMESTAMPDIFF(minute, pubdate, createtime) |
+--------------------------------------------+
| 0 |
| -1583 |
+--------------------------------------------+
2 rows in set (0.00 sec)
MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
MySQL datediff(date1,date2):两个日期相减date1 date2,返回天数。网站制作学习网整理
select datediff('2008-08-08', '2008-08-01'); - 7
select datediff('2008-08-01', '2008-08-08'); -7
MySQL timediff(time1,time2):两个日期相减time1 time2,返回time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00');- 08:08:08
select timediff('08:08:08', '00:00:00'); - 08:08:08
评论已关闭!