请问mysql的sql中如何计算两个datetime的差,精确到小时,谢谢
发布网友
发布时间:2022-04-07 18:00
我来回答
共4个回答
懂视网
时间:2022-04-07 22:21
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)
二,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)
MySQL中TIMESTAMPDIFF和TIMESTAMPADD函数的用法
标签:
热心网友
时间:2022-04-07 19:29
TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)
返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差。
其结果的单位由interval 参数给出。该参数必须是以下值的其中一个:
FRAC_SECOND 表示间隔是毫秒
SECOND 秒
MINUTE 分钟
HOUR 小时
DAY 天
WEEK 星期
MONTH 月
QUARTER 季度
YEAR 年
SELECT '年' AS `日期部分`, TIMESTAMPDIFF(YEAR, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '季度' AS `日期部分`, TIMESTAMPDIFF(QUARTER, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '月' AS `日期部分`, TIMESTAMPDIFF(MONTH, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '日' AS `日期部分`, TIMESTAMPDIFF(DAY, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '周' AS `日期部分`, TIMESTAMPDIFF(WEEK, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '时' AS `日期部分`, TIMESTAMPDIFF(HOUR, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '分' AS `日期部分`, TIMESTAMPDIFF(MINUTE, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
UNION ALL
SELECT '秒' AS `日期部分`, TIMESTAMPDIFF(SECOND, '2012-12-21', CURRENT_TIMESTAMP()) AS `数值`
;
+----------+----------+
| 日期部分 | 数值 |
+----------+----------+
| 年 | 1 |
| 季度 | 4 |
| 月 | 12 |
| 日 | 388 |
| 周 | 55 |
| 时 | 9328 |
| 分 | 559737 |
| 秒 | 33584279 |
+----------+----------+
8 rows in set (0.00 sec)
mysql> select CURRENT_TIMESTAMP();
+---------------------+
| CURRENT_TIMESTAMP() |
+---------------------+
| 2014-01-13 16:58:17 |
+---------------------+
1 row in set (0.00 sec)
热心网友
时间:2022-04-07 20:47
TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)
www.2cto.com
说明:
返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差。其结果的
单位由interval 参数给出。interval 的法定值同TIMESTAMPADD()函数说明中所列出的相同。
SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01');
interval可是:
SECOND 秒 SECONDS
MINUTE 分钟 MINUTES
HOUR 时间 HOURS
DAY 天 DAYS
MONTH 月 MONTHS
YEAR 年 YEARS追问不对啊,大哥,这个都是网上的,但是用不了啊?
追答报什么错误?
热心网友
时间:2022-04-07 22:22
精确到小时
select TIMESTAMPDIFF(Hour,'2014-01-01 14:00','2014-01-01 16:00');
精确到分钟
select TIMESTAMPDIFF(MINUTE,'2014-01-01 14:00','2014-01-01 16:00')