日期:2014-05-18  浏览次数:20578 次

怎么用sql根据当天时间查询连续登陆
比如我要根据今天的时间来查询用户或某个用户这7天来是否有连续登陆。

------解决方案--------------------
一个笨方法
SQL code


create table #loginlog(logintime datetime,u_id int)

insert into #loginlog select '2011-12-16',907
insert into #loginlog select '2011-12-17',907
insert into #loginlog select '2011-12-18',907
insert into #loginlog select '2011-12-14',1100
insert into #loginlog select '2011-12-15',1100
insert into #loginlog select '2011-12-16',1100
insert into #loginlog select '2011-12-13',1200
insert into #loginlog select '2011-12-14',1200
insert into #loginlog select '2011-12-16',1200
insert into #loginlog select '2011-12-17',1200
insert into #loginlog select '2011-12-18',1200
go
declare @date datetime
SET @date='2011-12-16'
select a.u_id from #loginlog a
join (select u_id  from #loginlog where logintime=DATEADD(D,-1,@date))b on b.u_id=a.u_id
join (select u_id  from #loginlog where logintime=DATEADD(D,-2,@date))c on c.u_id=a.u_id
where logintime=@date 


drop table #loginlog

------解决方案--------------------
select distinct u_id,count(*) as 连续登陆次数 from loginlog
where logintime between convert(varchar(10),DATEADD(DD,-3,'2011-12-18'),120) 
and CONVERT(varchar(10),'2011-12-18',120)
group by u_id
having COUNT(*)>=3