mysql 统计当个用户从当前时间连续登录天数,以及多用户某时间段,最长连续登录天数查询

1、当个用户当前连续登录天数查询
SELECT
GETINTO_USERID,
max(days) incessancy_days,
min(ot) start_date,
max(ot) end_date
FROM
(
SELECT
*
FROM
(
SELECT
GETINTO_USERID ,@cont_day := (
CASE
WHEN (
@last_uid = GETINTO_USERID
AND DATEDIFF(ot, @last_ot) = 1
) THEN
(@cont_day + 1)
WHEN (
@last_uid = GETINTO_USERID
AND DATEDIFF(ot, @last_ot) < 1
) THEN
(@cont_day + 0)
ELSE
1
END
) AS days,
(
@cont_ix := (
@cont_ix +
IF (@cont_day = 1, 1, 0)
)
) AS cont_ix ,@last_uid := GETINTO_USERID ,@last_ot := ot ot
FROM
(
SELECT
GETINTO_USERID,
GETINTO_TIME AS ot
FROM
game_activity_statistics
WHERE
GETINTO_USERID = 199511
ORDER BY
GETINTO_USERID,
GETINTO_TIME
) AS t1,
(
SELECT
@last_uid := ‘’,
@last_ot := ‘’,
@cont_ix := 0,
@cont_day := 0
) AS t2
) p
WHERE
‘2018-10-26’<=p.ot and p.ot<=SYSDATE()
) t
GROUP BY
GETINTO_USERID;
2、多用户某时间段,最长连续登录天数查询
select GETINTO_USERID
,max(days) incessancy_days
,min(ot) start_date
,max(ot) end_date
from
(
select GETINTO_USERID
,@cont_day :=
(
case when (@last_uid = GETINTO_USERID and DATEDIFF(ot, @last_ot)=1) then
(@cont_day + 1)
when (@last_uid = GETINTO_USERID and DATEDIFF(ot, @last_ot)<1) then
(@cont_day + 0)
else 1
end
) as days
,(@cont_ix := (@cont_ix + if(@cont_day = 1, 1, 0))) AS cont_ix
,@last_uid := GETINTO_USERID
,@last_ot := ot ot
from
(
select GETINTO_USERID
,GETINTO_TIME as ot
from game_activity_statistics
where GETINTO_TIME >= ‘2017-10-11’
and GETINTO_TIME < ‘2019-10-11’
order by GETINTO_USERID ,GETINTO_TIME
) as t1,
(
select @last_uid := ‘’,
@last_ot := ‘’,
@cont_ix := 0,
@cont_day := 0
) as t2
) as t
group by GETINTO_USERID;

你可能感兴趣的:(mysql 统计当个用户从当前时间连续登录天数,以及多用户某时间段,最长连续登录天数查询)