函數TimeStampDiff()是MySQL本身提供的可以計算兩個時間間隔的函數,語法為:TIMESTAMPDIFF(FRAC_SECOND (microseconds),unit,datetime_expr1,datetime_expr2),其中unit單位有如下幾種,分別是: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) |
