MySQL查询GROUP BY日/月/年

2021-11-20 00:00:00 datetime sql date group-by mysql

是否可以进行一个简单的查询来计算我在确定的时间段内(例如年、月或日)有多少记录,具有 TIMESTAMP 字段,例如:

Is it possible to make a simple query to count how many records I have in a determined period of time like a year, month, or day, having a TIMESTAMP field, like:

SELECT COUNT(id)
FROM stats
WHERE record_date.YEAR = 2009
GROUP BY record_date.YEAR

甚至:

SELECT COUNT(id)
FROM stats
GROUP BY record_date.YEAR, record_date.MONTH

要有月度统计数据.

谢谢!

推荐答案

GROUP BY YEAR(record_date), MONTH(record_date)

查看日期和时间MySQL 中的函数.

相关文章