直接给个例子:
SELECT
DATE_FORMAT(releaseDate,"%Y年%m月") AS dates,
COUNT(*)
FROM some_table
GROUP BY DATE_FORMAT(releaseDate,"%Y年%m月")
ORDER BY DATE_FORMAT(releaseDate,"%Y年%m月") DESC
字段解释:
releaseDate 日期
DATE_FORMAT();按照格式对某个日期操作
ORDER BY 排序
DESC 降序
整段代码的意思是:
从some_table中 查询日期(releaseDate)(将该日期按照"%Y年%m月"格式)并作为dates,统计数量(count(*)),
按照日期分组 GROUP BY DATE_FORMAT(releaseDate,"%Y年%m月"),
并降序 ORDER BY DATE_FORMAT(releaseDate,"%Y年%m月") DESC
如果想按照日来分组统计,就在group by 和 select 那里,按照格式加多天,即 "%Y年%m月%d日"就可以了