GROUP BY month on DATETIME field

后端 未结 5 1251
我寻月下人不归
我寻月下人不归 2021-01-02 08:49

I have the following query in mysql:

SELECT title, 
       added_on 
FROM   title 

The results looks like this:

Somos Tão J         


        
相关标签:
5条回答
  • 2021-01-02 09:17

    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) 
    
    0 讨论(0)
  • 2021-01-02 09:28

    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 |
    +---------------------+---------------------+
    
    0 讨论(0)
  • 2021-01-02 09:31
    select count(*), date(timestamp) from title group by MONTHNAME(timestamp)
    
    0 讨论(0)
  • 2021-01-02 09:32
    select 
           count(*) as Count,
           MONTH(timestamp)+"-"+YEAR(timestamp) as Month 
    from 
           title 
    GROUP BY 
            MONTH(tumestamp);
    
    0 讨论(0)
  • 2021-01-02 09:39
    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 :)

    0 讨论(0)
提交回复
热议问题