MYSQL计算时间间隔——————TimeStampDiff()



函数TimeStampDiff()是MySQL本身提供的可以计算两个时间间隔的函数,语法为TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)其中unit单位有如下几种,分别是FRAC_SECOND (microseconds),SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER, orYEAR。

例如

mysql> select timestampdiff(week,’2009-01-24′,’2009-06-20′);
+———————————————————–+
| timestampdiff(week,’2009-01-24′,’2009-06-20′)       |
+———————————————————–+
|                                                                       21|
+———————————————————–+
1 row in set (0.00 sec)mysql> select timestampdiff(day,’2009-01-24′,’2009-06-20′);
+———————————————————-+
| timestampdiff(day,’2009-01-24′,’2009-06-20′)        |
+———————————————————-+
|                                                                    147|
+———————————————————-+
1 row in set (0.00 sec)mysql> select timestampdiff(month,’2009-01-24′,’2009-06-20′);
+————————————————————-+
| timestampdiff(month,’2009-01-24′,’2009-06-20′)       |
+————————————————————-+
|                                                                           4|
+————————————————————-+
1 row in set (0.00 sec)mysql> select timestampdiff(year,’2009-01-24′,’2009-06-20′);
+———————————————————–+
| timestampdiff(year,’2009-01-24′,’2009-06-20′)        |


+———————————————————–+
|                                                                         0|
+———————————————————–+
1 row in set (0.00 sec)

你可能感兴趣的:(MYSQL计算时间间隔——————TimeStampDiff())