1

我有一个带有字段 uid (INT) 和 TIME (TIMESTAMP) 的 MySQL 表,其中注册了站点上的用户登录。

接收在 2 天或更长时间内进入网站的用户数量的最聪明的方法是什么?是否存在比我使用的下面的片段更好的东西?

SELECT count(*) FROM
   (SELECT uid, count(*) as DistinctDayCount FROM 
       (SELECT uid, Time FROM Log GROUP BY uid, DATE(Time)) AS DistinctDays 
   GROUP BY uid) AS DaysCount
WHERE DistinctDayCount > 1
4

2 回答 2

1

你可以用少一个子查询来做到这一点:

select count(*)
from (select uid, count(distinct date(time)) as DistinctDayCount
      from log
      group by uid
     ) t
WHERE DistinctDayCount > 1

不过,老实说,我会使用如下查询查看每个计数的数字:

select DistinctDayCount, count(*), min(uid), max(uid)
from (select uid, count(distinct date(time)) as DistinctDayCount
      from log
      group by uid
     ) t
group by DistinctDayCount
order by 1

并给出示例用户 ID,您可以进一步调查minmax通常在“有多少用户登录超过 1 天”之后的下一个问题是“他们登录天数的分布是什么”。

于 2013-01-24T12:22:47.570 回答
0
select count(*) from
(
SELECT uid, min(Time), Max(Time) 
FROM Log 
GROUP BY uid 
HAVING DATEDIFF(max(Time),min(Time))>1
) t
于 2013-01-24T12:36:48.513 回答