简体   繁体   English

如何在MySQL中计算日期时间戳之间的总时间

[英]How to calculate total time between datetime stamps in MySQL

Lets say I have this record: 可以说我有这个记录:

**Task Details Table: task_details**

id         task_id    user_id      task_type         date_time
1            2          7            1               2015-08-23 10:05:06
2            2          7            2               2015-08-23 10:05:33
3            2          7            1               2015-08-23 10:05:53
4            2          7            2               2015-08-24 10:10:53
5            2          7            3               2015-08-24 10:11:50
6            3          9            1               2015-08-23 10:08:06
7            3          9            2               2015-08-23 10:15:33

NB: task_type mean is 1 = running, 2 = Pause, 3 = stop 注意: task_type的平均值是1 =运行,2 =暂停,3 =停止

I want to get total pause time in MySql. 我想在MySql中获得总的暂停时间。 how can select the date_timein total pause time convert in (hours:minutes). 如何选择date_time的总暂停时间转换为(小时:分钟)。

SELECT 
  SEC_TO_TIME( SUM( TIME_TO_SEC( `date_time` ) ) ) AS psuse_time  
FROM task_details where task_type = 2 GROUP BY task_id, user_id

Can any body offer some explanation as to what might be going on here. 任何机构都可以对这里可能发生的情况提供一些解释。

You can use the Unixtime functions in MySQL to do your calculations. 您可以在MySQL中使用Unixtime函数进行计算。

mysql> SELECT UNIX_TIMESTAMP('2015-08-24 10:10:53') - UNIX_TIMESTAMP('2015-08-23 10:05:53');
+-------------------------------------------------------------------------------+
| UNIX_TIMESTAMP('2015-08-24 10:10:53') - UNIX_TIMESTAMP('2015-08-23 10:05:53') |
+-------------------------------------------------------------------------------+
|                                                                         86700 |
+-------------------------------------------------------------------------------+
1 row in set (0.00 sec)

You could use MIN() and MAX() maybe to get the earliest and latest dates? 您可以使用MIN()和MAX()来获取最早和最新的日期吗?

mysql> SELECT UNIX_TIMESTAMP(MAX(datestamp)) - UNIX_TIMESTAMP(MIN(datestamp)) FROM table;
+-------------------------------------------------------------------------------+
| UNIX_TIMESTAMP(MAX(datestamp)) - UNIX_TIMESTAMP(MIN(datestamp)) |
+-------------------------------------------------------------------------------+
|                                                                         86700 |
+-------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Are you trying to calculate the difference between all of them or only those who are next to each other in the list? 您是要计算所有这些对象之间的差异,还是只计算列表中彼此相邻的那些对象之间的差异?

I'll update this answer with more info if requ'd. 如果需要,我将使用更多信息更新此答案。

EDIT 编辑

Updated with conditional. 有条件更新。

SELECT UNIX_TIMESTAMP(MAX(datestamp)) - UNIX_TIMESTAMP(MIN(datestamp)) FROM task_details WHERE task_type = 2 GROUP BY task_id ORDER BY datestamp;

Try this out. 试试看 The dummy data above didn't have many rows. 上面的虚拟数据没有很多行。 I tested briefly. 我做了简短的测试。 Please report back. 请报告。

EDIT #2 编辑#2

I just added some additional rows to the test table I have and the query worked well. 我刚刚在测试表中添加了一些额外的行,查询效果很好。

Note that 'datetime' is a reserved word in MySQL and you should consider renaming that column. 注意,“ datetime”是MySQL中的保留字,您应该考虑重命名该列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM