在MYSQL中跨越多行的时间戳之间的累计时间总和的最佳方法

时间:2014-08-26 12:56:56

标签: mysql date timestamp

我想知道用户在某一天,昨天,当前周(从星期一开始)和当月登录的时间总和(以秒为单位)。

我喜欢白天和星期,但不是月份。

这是我的数据集:

| ID   | userID   | blank| inDateTime          | outDateTime         |
 --------------------------------------------------------------------
| 5451 |      134 |      | 2014-07-30 13:44:26 | 2014-07-31 03:11:03 |
| 5510 |      134 |      | 2014-07-31 12:18:15 | 2014-07-31 13:18:24 |
| 5526 |      134 |      | 2014-07-31 17:01:48 | 2014-08-01 02:45:54 |
| 5602 |      134 |      | 2014-08-01 23:34:12 | 2014-08-02 00:11:57 |
| 5613 |      134 |      | 2014-08-02 01:11:02 | 2014-08-02 04:46:02 |
| 5697 |      134 |      | 2014-08-03 16:31:07 | 2014-08-03 22:01:31 |
| 5712 |      134 |      | 2014-08-03 22:05:51 | 2014-08-03 22:07:15 |
| 5751 |      134 |      | 2014-08-04 13:42:41 | 2014-08-05 02:28:13 |
| 5807 |      134 |      | 2014-08-05 11:55:56 | 2014-08-06 02:09:18 |
| 5871 |      134 |      | 2014-08-06 12:00:00 | 2014-08-06 18:09:45 |
| 5911 |      134 |      | 2014-08-06 22:24:18 | 2014-08-07 02:12:53 |
| 6124 |      134 |      | 2014-08-10 14:32:56 | 2014-08-10 22:02:25 |
| 6153 |      134 |      | 2014-08-10 23:13:38 | 2014-08-10 23:24:55 |
| 6182 |      134 |      | 2014-08-11 12:53:57 | 2014-08-12 00:44:11 |
| 6230 |      134 |      | 2014-08-12 11:32:31 | 2014-08-12 19:06:25 |
| 6269 |      134 |      | 2014-08-13 00:41:27 | 2014-08-13 03:48:36 |
| 6303 |      134 |      | 2014-08-13 12:38:28 | 2014-08-14 02:00:22 |
| 6358 |      134 |      | 2014-08-14 11:50:09 | 2014-08-15 01:19:02 |
| 6418 |      134 |      | 2014-08-15 10:59:32 | 2014-08-15 15:07:35 |
| 6528 |      134 |      | 2014-08-17 13:07:48 | 2014-08-17 15:37:36 |
| 6543 |      134 |      | 2014-08-17 19:21:09 | 2014-08-18 00:31:00 |
| 6579 |      134 |      | 2014-08-18 10:53:36 | 2014-08-18 23:46:23 |
| 6662 |      134 |      | 2014-08-19 13:51:47 | 2014-08-20 01:00:13 |
| 6704 |      134 |      | 2014-08-20 10:18:07 | 2014-08-21 01:25:22 |
| 6788 |      134 |      | 2014-08-21 16:19:16 | 2014-08-22 01:03:32 |
| 6828 |      134 |      | 2014-08-22 10:23:35 | 2014-08-22 23:33:31 |
| 6952 |      134 |      | 2014-08-24 12:40:03 | 2014-08-24 18:56:43 |
| 6972 |      134 |      | 2014-08-24 20:23:40 | 2014-08-25 00:01:22 |
| 7061 |      134 |      | 2014-08-26 05:31:51 | 2014-08-26 05:42:59 |
| 7062 |      134 |      | 2014-08-26 06:08:16 | 2014-08-26 06:08:19 |
| 7067 |      134 |      | 2014-08-26 07:55:26 | 2014-08-26 07:55:28 |
| 7068 |      134 |      | 2014-08-26 07:55:32 | 2014-08-26 07:55:34 |
| 7069 |      134 |      | 2014-08-26 07:55:37 | 2014-08-26 07:55:40 |
| 7070 |      134 |      | 2014-08-26 07:55:50 | 2014-08-26 07:55:54 |
| 7071 |      134 |      | 2014-08-26 07:57:13 | 2014-08-26 07:57:15 |
| 7072 |      134 |      | 2014-08-26 07:57:47 | 2014-08-26 07:57:51 |
| 7073 |      134 |      | 2014-08-26 07:58:41 | 2014-08-26 07:58:44 |

当我用于今天的计算时:

SELECT SUM( outDateTime - inDateTime ) AS minToday
FROM time_log
WHERE userID =134
AND DATE(  `inDateTime` ) = DATE( NOW( ) ) 
AND outDateTime !=  '0000-00-00 00:00:00'

我得到1131秒的正确答案。

当我尝试用这个月做的时候:

SELECT SUM( outDateTime - inDateTime ) AS minToday
FROM time_log
WHERE userID =134
AND  `inDateTime` > DATE_SUB( NOW( ) , INTERVAL 1 
MONTH ) 
AND outDateTime !=  '0000-00-00 00:00:00'

我得到:84382713

这超过2年半的时间,并且无法正确。

为什么正确计算天数时间戳并且月份没有?

我应该如何正确查询此类数据?


我修改了第二个查询,将总秒数作为正整数。我还将时间选择修改为仅选择当月的天数。

    SELECT SUM( TIMESTAMPDIFF(SECOND,inDateTime, outDateTime ) ) AS minToday
    FROM time_log
    WHERE userID =".$id."
    AND YEAR(`inDateTime`) = YEAR(CURDATE()) AND MONTH(`inDateTime`) = MONTH(CURDATE()) 
    AND outDateTime !=  '0000-00-00 00:00:00'",true);

1 个答案:

答案 0 :(得分:1)

你可以直接像你一样尝试做数学。您必须转换为unix时间戳,以便在减去或使用timestampditt函数时有两个整数。

SELECT SUM( UNIX_TIMESTAMP(outDateTime) - UNIX_TIMESTAMP(inDateTime) ) AS minToday
FROM time_log
WHERE userID =134
AND  `inDateTime` > DATE_SUB( NOW( ) , INTERVAL 1 
MONTH ) 
AND outDateTime !=  '0000-00-00 00:00:00'

或者你可以做:(首选)

SELECT TIMESTAMPDIFF(MINUTE,outDateTime inDateTime ) AS minToday
FROM time_log
WHERE userID =134
AND  `inDateTime` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) 
AND outDateTime !=  '0000-00-00 00:00:00'

不是你的字段名称" minToday"对于它们中的任何一个都是不正确的,因为第一个替代结果在几秒钟内产生,第二个替代结果在本月以分钟形式产生。

要检查的最后一件事是你的陈述。 如果你做了

select * from time_log
WHERE userID =134
AND  `inDateTime` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) 
AND outDateTime !=  '0000-00-00 00:00:00'

您是否只获得预期的行?