return count 0 with mysql group by

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

database table like this

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


        
相关标签:
4条回答
  • 2020-12-06 18:39

    This:

    SELECT  id, COUNT(suburb_id)
    FROM    (
            SELECT  1 AS id
            UNION ALL
            SELECT  2 AS id
            UNION ALL
            SELECT  3 AS id
            UNION ALL
            SELECT  4 AS id
            ) ids
    LEFT JOIN
            suburbs s
    ON      s.suburb_id = ids.id
    GROUP BY
            id
    

    or this:

    SELECT  id,
            (
            SELECT  COUNT(*)
            FROM    suburb
            WHERE   suburb_id = id
            )
    FROM    (
            SELECT  1 AS id
            UNION ALL
            SELECT  2 AS id
            UNION ALL
            SELECT  3 AS id
            UNION ALL
            SELECT  4 AS id
            ) ids
    

    This article compares performance of the two approaches:

    • Aggregates: subqueries vs. GROUP BY

    , though it does not matter much in your case, as you are querying only 4 records.

    0 讨论(0)
  • 2020-12-06 18:42

    A GROUP BY needs rows to work with, so if you have no rows for a certain category, you are not going to get the count. Think of the where clause as limiting down the source rows before they are grouped together. The where clause is not providing a list of categories to group by.

    What you could do is write a query to select the categories (suburbs) then do the count in a subquery. (I'm not sure what MySQL's support for this is like)

    Something like:

    SELECT 
      s.suburb_id,
      (select count(*) from suburb_data d where d.suburb_id = s.suburb_id) as total
    FROM
      suburb_table s
    WHERE
      s.suburb_id in (1,2,3,4)
    

    (MSSQL, apologies)

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-06 18:44

    @geofftnz's solution works great if all conditions are simple like in this case. But I just had to solve a similar problem to generate a report where each column in the report is a different query. When you need to combine results from several select statements, then something like this might work.

    You may have to programmatically create this query. Using left joins allows the query to return rows even if there are no matches to suburb_id with a given id. If your db supports it (which most do), you can use IFNULL to replace null with 0:

    select IFNULL(a.count,0), IFNULL(b.count,0), IFNULL(c.count,0), IFNULL(d.count,0)
    from (select count(suburb_id) as count from suburbs where id=1 group by suburb_id) a,
     left join (select count(suburb_id) as count from suburbs where id=2 group by suburb_id) b on a.suburb_id=b.suburb_id
     left join (select count(suburb_id) as count from suburbs where id=3 group by suburb_id) c on a.suburb_id=c.suburb_id
     left join (select count(suburb_id) as count from suburbs where id=4 group by suburb_id) d on a.suburb_id=d.suburb_id;
    

    The nice thing about this is that (if needed) each "left join" can use slightly different (possibly fairly complex) query.

    Disclaimer: for large data sets, this type of query might have not perform very well (I don't write enough sql to know without investigating further), but at least it should give useful results ;-)

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