失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > mysql统计用户周活跃_MySql查询脚本用于计算每个月的活跃用户数

mysql统计用户周活跃_MySql查询脚本用于计算每个月的活跃用户数

时间:2021-08-21 12:05:50

相关推荐

mysql统计用户周活跃_MySql查询脚本用于计算每个月的活跃用户数

你可以用一招来做到这一点.从每个日期添加一个月,然后您可以使用union all和聚合计数:

select year(logindate), month(logindate), count(distinct userid)

from ((select logindate, userid

from logger

) union all

(select date_add(longdate, interval 1 month), userid

from logger

)

) l

group by year(logindate), month(logindate)

order by 1, 2;

编辑:

哦,我误解了这个问题.您需要连续两个月才能成为活跃用户.我知道用户登录会让用户活跃两个月.好的,您可以通过加入或存在来解决此问题:

select year(l.logindate), month(l.logindate), count(distinct l.userid)

from logger l

where exists (select 1

from logger l2

where l2.userid = l.userid and

year(date_sub(l.logindate, interval 1 month)) = year(l2.logindate) and

month(date_sub(l.logindate, interval 1 month)) = month(l2.logindate)

)

group by year(l.logindate), month(l.logindate);

如果觉得《mysql统计用户周活跃_MySql查询脚本用于计算每个月的活跃用户数》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。