MySQL 查询 GROUP BY 日/月/年

2022-01-30 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 中的函数.

相关文章