MySQL中TIMESTAMPDIFF和TIMESTAMPADD函數的用法

一,TIMESTAMPDIFFmysql

語法:sql

TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)。date

說明:select

返回日期或日期時間表達式datetime_expr1 和datetime_expr2the 之間的整數差。其結果的單位由interval 參數給出。該參數必須是如下值的其中一個:語法

FRAC_SECOND。表示間隔是毫秒im

  1. SECOND。秒
  2. MINUTE。分鐘
  3. HOUR。小時
  4. DAY。天
  5. WEEK。星期
  6. MONTH。月
  7. QUARTER。季度
  8. YEAR。年

使用以下:datetime

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)time

 

二,TIMESTAMPADD參數

 

語法:

TIMESTAMPADD(interval,int_expr,datetime_expr)

 

說明:

將整型表達式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)

相關文章
相關標籤/搜索