mysql 查询某时间段内连续n天登陆人数


//30多万条数据0.4秒 不到,查询某时间段内连续登陆n天的用户,1132条
SET @wy=0;
SELECT DISTINCT uid
FROM (SELECT MAX(date)-MIN(date) less,uid
    FROM (SELECT date-rn diff, uid, date, rn
        FROM (SELECT @wy:=@wy+1 rn, uid,
            datediff(login_time,'1971-01-01') date,login_time
            FROM (SELECT date(LOGINDATE) login_time, GUID uid FROM login_115_tab
                WHERE LOGINDATE>='2016-03-01 00:00:00' AND LOGINDATE <'2016-03-30 00:00:00'
                GROUP BY GUID, date(LOGINDATE)
            ORDER BY uid, date(login_time)
            ) as a
        )as b
    )as c
GROUP BY diff,uid
)as d
WHERE less>=6;

你可能感兴趣的:(菜鸟记)