Mysql: Group by Hour, 0 if no data

前端 未结 3 602
时光说笑
时光说笑 2021-01-15 11:08

I have the following query:

SELECT count(*) as \'totalCalls\', HOUR(`end`) as \'Hour\'
FROM callsDataTable 
WHERE company IN (
    SELECT number 
    FROM pr         


        
3条回答
  •  礼貌的吻别
    2021-01-15 11:55

    The generation of values from nothing is not an easy job (usually it's not even possible) in MySQL.

    I suggest a simpler approach:

    1. generate in the client code a list of 24 entries (totalCalls, Hour) with 0 as totalCalls and the hours (from 0 to 23) as Hour. This is an easy task in any programming language.
    2. run the query you already have, get the values it returns and use them to overwrite the empty values generated on the previous step.
    3. enjoy.

提交回复
热议问题