SQL time difference within single table

前端 未结 1 886
盖世英雄少女心
盖世英雄少女心 2021-01-22 14:42


I have a single MySQL table with login data of users.

user |  date               | type
-----+---------------------+------
1    | 2011-01-05 08:00:00 | login         


        
相关标签:
1条回答
  • 2021-01-22 15:29
    SELECT x.*, TIMEDIFF(x.logout_date, x.login_date) as duration
    FROM
    (
    SELECT a.user_id, a.`date` as logout_date, 
    (SELECT MAX(b.`date`) FROM table1 b WHERE b.`date` <a.`date` 
    and b.user=a.user and b.type = 'login') as login_date    
    FROM table1 a WHERE a.type ='logout'
    )x
    
    0 讨论(0)
提交回复
热议问题