MySQL内置函数——关于时间戳转换和计算函数
0
111
mental
2019/7/24 11:04:58
MySQL内置关于时间戳的计算和转换函数介绍
TIMESTAMP函数
语法:
[pre]
TIMESTAMP(datetime_expr)
TIMESTAMP(datetime_expr,expr)
[/pre]
说明:
一个参数时,是将datetime_expr转换成时间戳格式(timestamp),两个参数时,是将datetime_expr转换成时间戳并加上expr
使用举例:
[pre]
mysql> select timestamp('2019-07-20 08:00:00');
+----------------------------------+
| timestamp('2019-07-20 08:00:00') |
+----------------------------------+
| 2019-07-20 08:00:00 |
+----------------------------------+
1 row in set
mysql> select timestamp('2019-07-20 08:00:00', '01');
+----------------------------------------+
| timestamp('2019-07-20 08:00:00', '01') |
+----------------------------------------+
| 2019-07-20 08:00:01 |
+----------------------------------------+
1 row in set
mysql> select timestamp('2019-07-20 08:00:00', '01:01');
+-------------------------------------------+
| timestamp('2019-07-20 08:00:00', '01:01') |
+-------------------------------------------+
| 2019-07-20 09:01:00 |
+-------------------------------------------+
1 row in set
mysql> select timestamp('2019-07-20 08:00:00', '01:01:01');
+----------------------------------------------+
| timestamp('2019-07-20 08:00:00', '01:01:01') |
+----------------------------------------------+
| 2019-07-20 09:01:01 |
+----------------------------------------------+
1 row in set
mysql> select timestamp('2019-07-20 08:00:00', '10 01:01:01');
+-------------------------------------------------+
| timestamp('2019-07-20 08:00:00', '10 01:01:01') |
+-------------------------------------------------+
| 2019-07-30 09:01:01 |
+-------------------------------------------------+
1 row in set
mysql> select timestamp('2019-07-20 08:00:00', '01 10');
+-------------------------------------------+
| timestamp('2019-07-20 08:00:00', '01 10') |
+-------------------------------------------+
| 2019-07-21 18:00:00 |
+-------------------------------------------+
1 row in set
[/pre]
TIMESTAMPDIFF函数
语法:
[pre]
TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)
[/pre]
说明:
返回日期或日期时间表达式datetime_expr1 和datetime_expr2 之间的整数差。其结果的单位由interval 参数给出。该参数必须是以下值的其中一个:
[pre]
FRAC_SECOND 表示间隔是毫秒
SECOND 秒
MINUTE 分钟
HOUR 小时
DAY 天
WEEK 星期
MONTH 月
QUARTER 季度
YEAR 年
[/pre]
使用如下:
[pre]
mysql> select TIMESTAMPDIFF(day,'2019-07-10','2019-07-20');
+----------------------------------------------+
| TIMESTAMPDIFF(day,'2019-07-10','2019-07-20') |
+----------------------------------------------+
| 10 |
+----------------------------------------------+
1 row in set
mysql> select TIMESTAMPDIFF(MINUTE,'2019-07-15 09:00:00','2019-07-20 19:00:00');
+-------------------------------------------------------------------+
| TIMESTAMPDIFF(MINUTE,'2019-07-15 09:00:00','2019-07-20 19:00:00') |
+-------------------------------------------------------------------+
| 7800 |
+-------------------------------------------------------------------+
1 row in set
[/pre]
TIMESTAMPADD函数
语法:
[pre]
TIMESTAMPADD(interval,int_expr,datetime_expr)
[/pre]
说明:
将整型表达式int_expr添加到日期或日期时间表达式 datetime_expr中。式中的nterval和上文中列举的取值是一样的。
例如,int_expr为MINUTE时,是将datetime_expr时间增加int_expr分钟。
[pre]
mysql> select TIMESTAMPADD(MINUTE,60,'2019-07-20 21:00:00');
+-----------------------------------------------+
| TIMESTAMPADD(MINUTE,60,'2019-07-20 21:00:00') |
+-----------------------------------------------+
| 2019-07-20 22:00:00 |
+-----------------------------------------------+
1 row in set
[/pre]