-- 以下都是SQL数据库语法
-- 建立表格和测试数据
/*
-- 用户信息表
create table LoginUser(UserId varchar(6),UserName varchar(20),PassWord varchar(20))
insert into LoginUser
select '1001','张三','111111' union all
select '1002','李四','222222' union all
select '1003','王五','333333'
-- 登录状态表
create table LoginSys(UserId varchar(6),UserName varchar(20),LoginTime datetime,LoginMac varchar(36),OnLine bit)
insert into LoginSys
select '1001','张三','2018-05-17 09:17:55.463','111111',1 union all
select '1002','李四','2018-05-17 09:18:18.060','222222',1
*/
-- 查询测试
declare @UserId varchar(6),@PassWord varchar(20),@LoginTime datetime,@LoginMac varchar(36)
set @UserId = '1001'
set @PassWord = '111111'
set @LoginTime = getdate()
set @LoginMac = '111111'
select a.UserId,a.UserName,a.PassWord,b.LoginTime,b.LoginMac,b.OnLine,
case when b.OnLine is null or b.OnLine = 0 then 1
-- 没登录过或者已经退出登录
when b.LoginMac = @LoginMac then 1
-- 在本机登录,可以再次登录
when dateadd(mi,5,getdate()) >= LoginTime then 1 -- 超过5分钟没刷新在线时间
else 0
end as LoginPass
from LoginUser as a
left join LoginSys as b on a.UserId=b.UserId
where a.UserId=@UserId
[此贴子已经被作者于2018-5-17 11:32编辑过]