MySQL Query GROUP BY day / month / year

前端 未结 14 2363
野趣味
野趣味 2020-11-22 06:37

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

14条回答
  •  太阳男子
    2020-11-22 07:02

    try this one

    SELECT COUNT(id)
    FROM stats
    GROUP BY EXTRACT(YEAR_MONTH FROM record_date)
    

    EXTRACT(unit FROM date) function is better as less grouping is used and the function return a number value.

    Comparison condition when grouping will be faster than DATE_FORMAT function (which return a string value). Try using function|field that return non-string value for SQL comparison condition (WHERE, HAVING, ORDER BY, GROUP BY).

提交回复
热议问题