MySQL: filling empty fields with zeroes when using GROUP BY

后端 未结 4 397
孤城傲影
孤城傲影 2020-12-30 09:49

I\'ve got MySQL table

CREATE TABLE cms_webstat (
    ID int NOT NULL auto_increment PRIMARY KEY,
    TIMESTAMP_X timestamp DEFAULT CURRENT_TIMESTAMP,
    # .         


        
4条回答
  •  情歌与酒
    2020-12-30 10:26

    This is just the 'why it is not returning` part. Marcus' answer covers the 'how to' part.

    The SQL

    SELECT 
        hour(TIMESTAMP_X) as HOUR 
        , count(*) AS HOUR_STAT 
    FROM cms_webstat 
    GROUP BY HOUR 
    ORDER BY HOUR DESC 
    

    gets the count of the records per hour, for the timestamps present in the table

    It does not give the details of what is not present in the table. Since there is no recors for the timestamp corresponding to the hour 8 (from your example) the SQL does not return any records.

提交回复
热议问题