检索在线用户通过登录和注销记录(Retrieving Online Users By Login a

2019-10-23 10:53发布

考虑用户日志表如上。 我保存用户的登录或注销运行到这个表。 我怎么可以检索在线用户和登录时间?

Answer 1:

事情是这样的:

select * from UserLog l1
where Operation = 'Enter' and 
      not exists(select * from UserLog l2 
                 where l1.user = l2.user and 
                       l2.Operation = 'Exit' and 
                       l2.Time > l1.Time)


Answer 2:

我想,这应该为你工作:

SELECT *
FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY UL.id ORDER BY UL.Time DESC) AS RN, UL.*
    FROM dbo.UserLog AS UL
    ) AS T
WHERE T.RN = 1
    AND T.Operation = 'Enter';

仅仅通过用户名分区表格和上次动作时间排序-如果它的Enter ,那么他必须在线。



文章来源: Retrieving Online Users By Login and Logout Records