0

输入

 userid           login time                               logout time
----------------------------------------------------------------------------
   1          2012-08-01 16:08:26                     2012-08-01 16:08:29
   2          2012-08-01 16:22:49                     2012-08-01 16:25:44
   3          2012-08-01 16:08:26                     2012-08-01 16:08:29
   3          2012-08-01 16:22:49                     2012-08-01 16:25:44
   3          2012-08-01 16:08:26                     2012-08-01 16:08:29
   4          2012-08-01 16:22:49                     2012-08-01 16:25:44

输出:

 userid        date         total time difference b/w login time and logout time
---------------------------------------------------------------------------------
   1        2012-08-01                         00:08:29
   2        2012-08-01                         1:25:44
   3        2012-08-01                         00;55;5
   4        2012-08-01                         1:25:44

我试过的查询是:

SELECT distinct t.user_id, DATE_FORMAT(t.login_time,\'%d %b %Y\') AS datez,
       SEC_TO_TIME(SUM(TIME_TO_SEC(t.logout_time) - TIME_TO_SEC(t.login_time))) AS timediffe
from login_log t 
where user_id=5 
  AND login_time between '2012-08-01' AND '2012-08-2' 
GROUP BY t.user_id,datez
4

4 回答 4

4

我认为你只需要使用TIMEDIFF函数:

SELECT t.user_id,
       DATE_FORMAT(t.login_time,'%d %b %Y') AS datez,
       SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(t.logout_time, t.login_time)))) AS timediffe
FROM tic_login_log t
WHERE user_id = 5
      AND login_time between '2012-08-01' AND '2012-08-2'
GROUP BY t.user_id,datez;
于 2012-08-27T08:07:17.343 回答
1

根据“2012-08-01”的相关输出

select userid, date(login_time) as Date, sum(timediff(logout_time-login_time) as
'Logged in Time' where date(login_time)='2012-08-01' group by userid;

日期虎钳每个用户的总登录时间

select userid, date(login_time) as Date, sum(timediff(logout_time-login_time) as
'Logged in Time'    group by userid,date(login_time);

每个用户的历史总登录时间

select userid, date(login_time) as Date, sum(timediff(logout_time-login_time) as
'Logged in Time' group by userid;
于 2012-08-27T09:30:46.983 回答
0

请使用此方法查找时间差:

select TIMEDIFF(NOW(), '2012-06-20 06:24:43')
于 2012-08-27T08:19:26.470 回答
0

你可以使用TIMEDIFF()方法。

尝试这个 :

SELECT timediff(your_logout_time,your_login_time);

例子 :-

SELECT timediff('2012-08-02 16:08:29','2012-08-01 16:08:26');

输出 :-'24:00:03'

于 2012-08-27T08:34:49.063 回答