MySQL Query GROUP BY day / month / year

前端 未结 14 2528
野趣味
野趣味 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:09

    You can do this simply Mysql DATE_FORMAT() function in GROUP BY. You may want to add an extra column for added clarity in some cases such as where records span several years then same month occurs in different years.Here so many option you can customize this. Please read this befor starting. Hope it should be very helpful for you. Here is sample query for your understanding

    SELECT
        COUNT(id),
        DATE_FORMAT(record_date, '%Y-%m-%d') AS DAY,
        DATE_FORMAT(record_date, '%Y-%m') AS MONTH,
        DATE_FORMAT(record_date, '%Y') AS YEAR
    
    FROM
        stats
    WHERE
        YEAR = 2009
    GROUP BY
        DATE_FORMAT(record_date, '%Y-%m-%d ');
    

提交回复
热议问题