php - Calculating time difference between events in mysql -
assuming have table following structure :
how calculate time difference between events login
, logout
specific users? want aggregate of session times each user had(so aggregate of times between each login
, logout
sessions). aware datediff
can used i'm not sure syntax , how able use multiple users
the final out put :
+-------------+-------------------------+ | agent | total session time | +-------------+-------------------------+ | user - 194 | 00:30:00 | | user - 195 | 00:40:00 | +-------------+-------------------------+
my version outputs difference between last logout , login actions:
select t1.agent, timediff(t2.lot, t1.lit) (select agent, max(time) lit log event = 'login' group 1) t1 join (select agent, max(time) lot log event = 'logout' group 1) t2 on t1.agent = t2.agent group 1;
i used following table definition:
create table `log` ( `time` datetime default null, `agent` varchar(50) default null, `event` enum('login','logout') default null )
example data , output
data:
+---------------------+-------+--------+ | time | agent | event | +---------------------+-------+--------+ | 2015-07-23 15:40:18 | u1 | login | | 2015-07-23 14:40:24 | u2 | login | | 2015-07-23 16:40:34 | u1 | logout | | 2015-07-23 16:40:39 | u2 | logout | | 2015-07-23 16:44:19 | u2 | login | | 2015-07-23 16:46:25 | u2 | logout | +---------------------+-------+--------+
result of select:
+-------+--------------------------+ | agent | timediff(t2.lot, t1.lit) | +-------+--------------------------+ | u1 | 01:00:16 | | u2 | 00:02:06 | +-------+--------------------------+
Comments
Post a Comment