我有一个表格,其中包含跟踪人们登录时间的网络应用程序的记录。我的表格示例如下:
| user_id | date_time |
+---------+------------------+
| 0033 | 2012-11-22 10:33 | <- first login of 0033 on 2012-11-22
| 0034 | 2012-11-22 10:38 | <- first login of 0034 on 2012-11-22
| 0052 | 2012-11-22 10:43 | <- first login of 0052 on 2012-11-22
| 0052 | 2012-11-23 09:23 |
| 0066 | 2012-11-23 15:58 | <- first login of 0066 on 2012-11-23
| 0033 | 2012-11-23 16:14 |
我想要的是一张桌子,上面列出了每个日期第一次登录的人数,即:
| count | date |
+-------+------------+
| 3 | 2012-11-22 | <- there were 3 users that logged in for the first time on 2012-11-22
| 1 | 2012-11-23 |
我知道我只能通过
获取日期SELECT DATE(`date_time`) AS `date`
FROM `logging`
GROUP BY `date`
ORDER BY `date` ASC
我想在一个查询中获取第二个表,我知道这是可能的,我只是不知道如何。提前致谢
答案 0 :(得分:1)
您可以使用不相关的子查询来获取每个用户的第一个登录日期,然后将这些日期组合在一起以获取每天的首次登录次数。
SELECT dd, COUNT(*)
FROM (SELECT MIN(DATE(`date_time`)) AS dd
FROM `logging`
GROUP BY `user_id`) a
GROUP BY dd
ORDER BY dd;
答案 1 :(得分:1)
对于没有以前登录记录的user_ids,计算每天的登录次数:
select DATE(`date_time`) as `date`,
count(user_id)
from `logging` l1
where user_id not in (
select user_id from `logging` l2 where l1.user_id = l2.user_id and l2.date_time < l1.date_time)
group by DATE(`date_time`)
答案 2 :(得分:-1)
我认为你需要这个:
SELECT count(1) ,
DATE(`date_time`)
from my_table
group by DATE(`date_time`)
如果您需要日常登录的用户
Select
user_id, `date_time` from my_table group by DATE(`date_time`), user_id