我有一张表记录了当时登录的用户。我需要生成一个报告,以显示用户每月登录的次数。我该怎么做?
- Edit-- 我可以打印每个用户在一个月内出现X次,这对我来说没问题。但我想显示一个月度列表,显示一个月内登录5次的人数。
答案 0 :(得分:2)
SELECT
User
, DATE_FORMAT(LoggedInTime, '%Y-%m') As YearMonth
, COUNT(*) AS NumLogins
FROM user_logins
GROUP BY User, YearMonth
答案 1 :(得分:1)
此处可用的主要选项(IMO)是计算用户每月在群组中出现的次数,如下所示:
SELECT user_id,
count(datetime) as logins,
MONTH(datetime) as month
FROM table
GROUP BY 1, 3
对于mysql中的日期和时间函数,请查看手册here
答案 2 :(得分:1)
来自评论:“我想显示登录的用户数超过5次”
SELECT YearMonth, COUNT(User)
FROM (
SELECT
User
, DATE_FORMAT(LoggedInTime, '%Y-%m') As YearMonth
FROM user_logins
GROUP BY User, YearMonth
HAVING COUNT(*) > 5
)GROUP BY YearMonth