2015-06-20 75 views
1
  • enter image description here

考虑用户日志表如上。我将用户的登录或注销操作保存到此表中。我如何检索在线用户和登录时间?检索在线用户通过登录和注销记录

+0

你是什么意思在线?现在在线? –

+0

是的。我需要找到已经登录但尚未注销的用户。 – Behnam

回答

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) 
1

我认为这应该为你工作:

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,那么他就必须在线。