输入年周然后输出每天的总用户数

时间:2016-03-18 06:58:21

标签: mysql

我有users

id  |  username  |  password  |  created_at
---------------------------------------------------
1   |  foo       |  p4ssw0rd  |  2016-01-01 12:00:00
2   |  bar       |  123456    |  2016-01-02 12:00:00
....

如果仅输入年周,我如何实现此结果 (例如:'201605',2016年第5周)

total_users  |  date
------------------------
4            |  2016-01-04
6            |  2016-01-05
7            |  2016-01-06
10           |  2016-01-07
12           |  2016-01-08
12           |  2016-01-09
15           |  2016-01-10

如您所见,它每周显示total_users,每天一行。

我知道这个问题太多了,如果你能帮助我,我会提前感谢你。

2 个答案:

答案 0 :(得分:2)

使用MySQL WEEK功能。 MySQL WEEK()返回给定日期的周数

SELECT count(*) total_users,date(created_at) date
FROM `table` 
where concat(YEAR(created_at),WEEK(created_at))='201605'
group by date(created_at)

答案 1 :(得分:1)

SELECT 
COUNT(user) as Total_users,
date(created_at) as Date
FROM Users
GROUP BY date(created_at);

此致

相关问题