GROUP BY month on DATETIME field GROUP BY month on DATETIME field sql sql

GROUP BY month on DATETIME field


Could you try this?

select count(*), DATE_FORMAT(timestamp, "%Y-%m-01")from titlegroup by DATE_FORMAT(timestamp, "%Y-%m-01")

Please, note that MONTH() can't differentiate '2013-01-01' and '2014-01-01' as follows.

mysql> SELECT MONTH('2013-01-01'), MONTH('2014-01-01');+---------------------+---------------------+| MONTH('2013-01-01') | MONTH('2014-01-01') |+---------------------+---------------------+|                   1 |                   1 |+---------------------+---------------------+


SELECT Count(*),        Date(timestamp) FROM   title GROUP  BY Month(timestamp) 

Edit: And obviously if it matters to display the 1st day of the month in the results, you do the DATE_FORMAT(timestamp, "%Y-%m-01") thing mentioned in some other answers :)


The best bet is to group it by YEAR and then by MONTH. See below

SELECT Count(*), Date(timestamp) FROM title GROUP BY YEAR(timestamp), Month(timestamp)