return count 0 with mysql group by

后端 未结 4 869
死守一世寂寞
死守一世寂寞 2020-12-06 17:46

database table like this

============================
= suburb_id   |   value
= 1           |    2
= 1           |    3
= 2           |    4
= 3           |          


        
4条回答
  •  春和景丽
    2020-12-06 18:44

    Query:

    select case
             when total is null then 0
             else total
           end as total_with_zeroes,
           suburb_id
    from (SELECT COUNT(suburb_id) AS total, suburb_id 
            FROM suburbs 
           where suburb_id IN (1,2,3,4) 
        GROUP BY suburb_id) as dt
    

提交回复
热议问题