日期:2014-05-16  浏览次数:20781 次

如何检索一个月内连续登入的用户?
如何检索一个月内连续登入的用户? 

要求: 
1、只要有一次连续三天没登入就不符合要求。 
2、一天内一个用户可能会有连续几次的登入。 


举例loginlog表数据如下: 

uid logintime 
1 2007-10-1 
1 2007-10-1 
1 2007-10-2 
1 2007-10-3 
1 2007-10-4 
1 2007-10-5 
1 2007-10-5 
1 2007-10-5 
1 2007-10-7 
1 2007-10-10 
2 2007-10-1 
2 2007-10-2 
2 2007-10-4 
2 2007-10-5 
2 2007-10-5 
2 2007-10-7 
2 2007-10-9 
2 2007-10-10 
3 2007-10-1 
3 2007-10-2 
3 2007-10-3 
3 2007-10-4 
3 2007-10-5 
3 2007-10-5 
3 2007-10-8 
3 2007-10-9 
3 2007-10-10 
4 2007-10-1 
4 2007-10-5 
4 2007-10-5 
4 2007-10-9 
4 2007-10-10 
5 2007-10-1 
5 2007-10-3 
5 2007-10-5 
5 2007-10-6 
5 2007-10-10 

要求输出结果uid 为:1,2,3

------解决方案--------------------
楼上的,'loginlog'是什么啊? 'logintime'呢?
------解决方案--------------------
要求:
1、只要有一次连续三天没登入就不符合要求。
2、一天内一个用户可能会有连续几次的登入。


按照 你的要求不会有2出现的。

------解决方案--------------------
楼主说的是连续三天,如6,7,8三天都没有登录才不符合要求,而3,6,8三天是不连续的,故2算是连续登录了。


MYSQL没用过,不知道语法如何,给一段ACCESS的查询代码楼主试试:
SELECT loginlog.uid
FROM loginlog
GROUP BY loginlog.uid
HAVING (((Max(DateDiff("d",IIf(IsNull(DMax("[logintime]","loginlog","[uid]=" & [uid] & " and [logintime]<#" & [logintime] & "#")),"2007-10-1",DMax("[logintime]","loginlog","[uid]=" & [uid] & " and [logintime]<#" & [logintime] & "#")),[logintime])))<4));


注意,代码中指定有本月最小日期为"2007-10-1",楼主应按实际需要设置此值,另外,如果是最后三天没登录,只是这样也是不能反映出来的,因为不知楼主是否说这是在今日之前的一个月,这里也不妄作判断了。

------解决方案--------------------
SELECT loginlog.uid
FROM loginlog
GROUP BY loginlog.uid
HAVING (((Max((DateDiff("d",[logintime],IIf(IsNull(DMin("[logintime]","loginlog","[uid]=" & [uid] & " and [logintime]>#" & [logintime] & "#")),Date(),DMin("[logintime]","loginlog","[uid]=" & [uid] & " and [logintime]>#" & [logintime] & "#"))))))<4) AND ((Max((DateDiff("d",IIf(IsNull(DMax("[logintime]","loginlog","[uid]=" & [uid] & " and [logintime]<#" & [logintime] & "#")),date()-day(date())+1,DMax("[logintime]","loginlog","[uid]=" & [uid] & " and [logintime]<#" & [logintime] & "#")),[logintime]))))<4));

还有,应加上限定日期的条件如:where date()-logintime<30 之类。

------解决方案--------------------
拿一号来举例,一号登陆的日期(去掉重复的)为 1,2,3,4,5,7,10,我的思路是拿1来举例查询出三天内是否登陆,如果三天内有,筛选出1,然后是2,查询出2三天内是否登陆,如果有,筛选出2,等等等等,最后是7,查询出7三天内是否登陆,如有
筛选出7,这样10就没筛选出来,所以一号总共连续登陆6次(一个月,去掉10,实际是7次)。拿5号来举例6,10 之间三天没登陆,6没有筛选出来,实际连续登陆是三次,最后和他实际登陆的次数(去掉重复的,一号实际是7次,连续登陆是6次加上最后一次)就能筛选出来
select log2.uid from (select distinct log1.uid,log1.logintime from loginlog as log1 where 

log1.uid = (select distinct log4.uid from loginlog as log4 where log4.logintime=log1.logintime+1 and log1.uid =log4.uid)
or

log1.uid = (select distinct log5.uid from loginlog as log5 where log5.logintime=log1.logintime+2 and log1.uid =log5.uid)