GROUP BY month on DATETIME field

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 :)


Could you try this?

select count(*), DATE_FORMAT(timestamp, "%Y-%m-01")
from title
group 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 MONTHNAME(timestamp)

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) 

Tags:

Mysql

Sql