Mysql按周获取记录

时间:2017-05-29 13:57:48

标签: mysql database

我正在使用具有此类字段的用户表

id
username
email
created_at(unix timestamp)

现在我希望按月分组当前年度数据。 像这样的东西

month/week      count(*)
jan(1)          55
jan(2)          102
jan(3)          23
jan(4)          39
feb(1)          6
feb(2)          14
feb(3)          37
feb(4)          20
.               .
.               .
.               .
.               .
.               .

1 个答案:

答案 0 :(得分:1)

检查 DATE / TIME 功能

 SELECT MONTH(created_at), WEEK(created_at), count(*)
 FROM YourTable
 WHERE YEAR(created_at) = YEAR(NOW())
 GROUP BY MONTH(created_at), WEEK(created_at)