在MySQL應用時,常常要使用這兩個函數TIMESTAMPDIFF和TIMESTAMPADD。mysql
一,TIMESTAMPDIFFsql
語法:函數
TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)。date
說明:select
返回日期或日期時間表達式datetime_expr1 和datetime_expr2the 之間的整數差。其結果的單位由interval 參數給出。該參數必須是如下值的其中一個:語法
- FRAC_SECOND。表示間隔是毫秒
- SECOND。秒
- MINUTE。分鐘
- HOUR。小時
- DAY。天
- WEEK。星期
- MONTH。月
- QUARTER。季度
- YEAR。年
使用以下:im
- mysql> select TIMESTAMPDIFF(day,'2012-08-24','2012-08-30');
- +----------------------------------------------+
- | TIMESTAMPDIFF(day,'2012-08-24','2012-08-30') |
- +----------------------------------------------+
- | 6 |
- +----------------------------------------------+
- 1 row in set (0.00 sec)
- mysql> select TIMESTAMPDIFF(MINUTE,'2012-08-24 09:00:00','2012-08-30 12:00:00');
- +-------------------------------------------------------------------+
- | TIMESTAMPDIFF(MINUTE,'2012-08-24 09:00:00','2012-08-30 12:00:00') |
- +-------------------------------------------------------------------+
- | 8820 |
- +-------------------------------------------------------------------+
- 1 row in set (0.01 sec)
二,TIMESTAMPADDdatetime
語法:時間
TIMESTAMPADD(interval,int_expr,datetime_expr)time
說明:
將整型表達式int_expr 添加到日期或日期時間表達式 datetime_expr中。式中的interval和上文中列舉的取值是同樣的。
- mysql> select TIMESTAMPADD(MINUTE,8820,'2012-08-24 09:00:00');
- +-------------------------------------------------+
- | TIMESTAMPADD(MINUTE,8820,'2012-08-24 09:00:00') |
- +-------------------------------------------------+
- | 2012-08-30 12:00:00 |
- +-------------------------------------------------+
- 1 row in set (0.00 sec)