简体   繁体   中英

Total time difference between login time and logout time-MYSQL

INPUT

 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

OUTPUT:

 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

The query I have tried is:

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

I think you just need to use TIMEDIFF function as:

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;

According to required output in question for '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;

Date vise total logged in time for each user

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

Total logged in time in history for each user

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

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

select TIMEDIFF(NOW(), '2012-06-20 06:24:43')

You can use TIMEDIFF() method.

Try This :

SELECT timediff(your_logout_time,your_login_time);

Example :-

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

Output :- '24:00:03'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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