mysql报告:每天计数唯一登录

时间:2015-02-11 19:15:33

标签: mysql count report date-format

我(例如)一个mysql表"登录"有了这个内容:

user_id | last_login
1       | 2015-02-01 05:01:07
1       | 2015-02-01 12:42:09
2       | 2015-02-01 22:16:23
2       | 2015-02-02 15:45:23
2       | 2015-02-04 21:27:04
3       | 2015-02-04 06:25:45
4       | 2015-02-05 03:12:01

我的问题是:如何每天生成所有唯一身份用户的摘要。所以我会这样报告:

day        | count
2015-02-01 | 2
2015-02-02 | 1
2015-02-04 | 2
2014-02-05 | 1

此查询无效:

SELECT DATE_FORMAT( login_date, '%Y-%m-%d' ) AS `day`
(SELECT COUNT(DISTINCT(user_id)) AS count, DATE_FORMAT( login_date, '%Y-%m-%d' ) AS `sdate` WHERE `sdate` = day )
FROM `logins`
GROUP BY `day`
ORDER BY `day` // query not working.....

如何每天获得所有唯一身份用户的SQL总结...?

1 个答案:

答案 0 :(得分:6)

您可以在“group by”和“order by”子句中使用别名。此查询将为您提供报告:

select DATE_FORMAT(`last_login`, '%Y-%m-%d') as theDate, 
count(distinct(user_id)) as theCount from logins 
group by theDate 
order by theCount;

希望这会有所帮助。