复杂的mysql按时间在线查询订单

复杂的mysql按时间在线查询订单,mysql,Mysql,如何查询此表: (微定时器为浮点型,简单省去) microtimer(php),用户,操作 123456,艾伯特,登录 123459,胡安,登录 123467,玛丽亚,登录 123469,胡安,注销 123479,艾伯特,注销 123480,胡安,登录 123498,玛丽亚,注销 123499,胡安,注销 期望结果:(按在线时间排序) 在线时间,用户 (123498-123467)=31,玛丽亚 (123479-123456)=23,阿尔伯特 (123499-123480)=19,胡安 (12

如何查询此表:

(微定时器为浮点型,简单省去)

microtimer(php),用户,操作
123456,艾伯特,登录
123459,胡安,登录
123467,玛丽亚,登录
123469,胡安,注销
123479,艾伯特,注销
123480,胡安,登录
123498,玛丽亚,注销
123499,胡安,注销
期望结果:(按在线时间排序)

在线时间,用户
(123498-123467)=31,玛丽亚
(123479-123456)=23,阿尔伯特
(123499-123480)=19,胡安
(123469-123459)=10,胡安
===================================== 有一个问题

佩德罗45.4343
佩德罗12.2323
佩德罗7.45345353
me 90.06999393242798
me 12.1212121


所需的全局排序

代码:


您希望在每次登录后第一次注销记录。您可以使用相关子查询来获得它。那么,要区别对待:

select t.user, (logouttimer - logintimer)
from (select t.user, t.microtimer as logintimer, 
             (select t2.microtimer
              from thistable t2
              where t2.user = t.user and
                    t2.microtimer >= t.microtimer and
                    t2.operation = 'logout'
              order by t2.microtime asc
              limit 1
             ) as logout_timer
      from thistable t
      where t.operation = 'login'
     ) t;

我的愿望是排名用户
1.Pedro 45分钟在线
2.Maria 41分钟在线
3.Pedro 39分钟在线
4.Juan 36分钟在线
5.Maria 29分钟在线
是的,来自同一个人的多次登录/注销会生成各种条目,我的方法不好?
select t.user, (logouttimer - logintimer)
from (select t.user, t.microtimer as logintimer, 
             (select t2.microtimer
              from thistable t2
              where t2.user = t.user and
                    t2.microtimer >= t.microtimer and
                    t2.operation = 'logout'
              order by t2.microtime asc
              limit 1
             ) as logout_timer
      from thistable t
      where t.operation = 'login'
     ) t;