我正在编写一个MySQL查询,该查询输出按周创建的新用户数。
我的用户表是:
id | created_at
我的查询:
SELECT YEAR(created_at) AS Year, DATE_FORMAT(created_at, '%b %e') AS Week, COUNT(*) AS total
FROM users
GROUP BY Year, Week;
问题:
2019 | May 9 | 100
我希望对年和周进行排序。
答案 0 :(得分:2)
我认为您会发现函数YEARWEEK()
不仅对分组很有帮助,而且对订购也很有帮助。
此外,您对DATE_FORMAT()
的使用看起来不正确,因为您正在输出%e
(这是一个月的某天),但是您要按周分组?
SELECT DATE_FORMAT(created_at, '%Y %b %v') AS date, COUNT(*) AS total
FROM users
GROUP BY YEARWEEK(created_at)
ORDER BY YEARWEEK(created_at) DESC;