MySQL Query GROUP BY day / month / year

前端 未结 14 2605
野趣味
野趣味 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条回答
  •  猫巷女王i
    2020-11-22 07:14

    If you want to get a monthly statistics with row counts per month of each year ordered by latest month, then try this:

    SELECT count(id),
          YEAR(record_date),
          MONTH(record_date) 
    FROM `table` 
    GROUP BY YEAR(record_date),
            MONTH(record_date) 
    ORDER BY YEAR(record_date) DESC,
            MONTH(record_date) DESC
    

提交回复
热议问题