1. <tfoot id='X5Xlc'></tfoot>
    <legend id='X5Xlc'><style id='X5Xlc'><dir id='X5Xlc'><q id='X5Xlc'></q></dir></style></legend>

    <small id='X5Xlc'></small><noframes id='X5Xlc'>

    <i id='X5Xlc'><tr id='X5Xlc'><dt id='X5Xlc'><q id='X5Xlc'><span id='X5Xlc'><b id='X5Xlc'><form id='X5Xlc'><ins id='X5Xlc'></ins><ul id='X5Xlc'></ul><sub id='X5Xlc'></sub></form><legend id='X5Xlc'></legend><bdo id='X5Xlc'><pre id='X5Xlc'><center id='X5Xlc'></center></pre></bdo></b><th id='X5Xlc'></th></span></q></dt></tr></i><div id='X5Xlc'><tfoot id='X5Xlc'></tfoot><dl id='X5Xlc'><fieldset id='X5Xlc'></fieldset></dl></div>

        <bdo id='X5Xlc'></bdo><ul id='X5Xlc'></ul>

      使用 mysql group by 返回计数 0

      return count 0 with mysql group by(使用 mysql group by 返回计数 0)
        1. <tfoot id='DiEqH'></tfoot>

              <tbody id='DiEqH'></tbody>

            <small id='DiEqH'></small><noframes id='DiEqH'>

              <bdo id='DiEqH'></bdo><ul id='DiEqH'></ul>
              <legend id='DiEqH'><style id='DiEqH'><dir id='DiEqH'><q id='DiEqH'></q></dir></style></legend>
            • <i id='DiEqH'><tr id='DiEqH'><dt id='DiEqH'><q id='DiEqH'><span id='DiEqH'><b id='DiEqH'><form id='DiEqH'><ins id='DiEqH'></ins><ul id='DiEqH'></ul><sub id='DiEqH'></sub></form><legend id='DiEqH'></legend><bdo id='DiEqH'><pre id='DiEqH'><center id='DiEqH'></center></pre></bdo></b><th id='DiEqH'></th></span></q></dt></tr></i><div id='DiEqH'><tfoot id='DiEqH'></tfoot><dl id='DiEqH'><fieldset id='DiEqH'></fieldset></dl></div>

              • 本文介绍了使用 mysql group by 返回计数 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这样的数据库表

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

                查询是

                SELECT COUNT(suburb_id) AS total, suburb_id 
                  FROM suburbs 
                 where suburb_id IN (1,2,3,4) 
                GROUP BY suburb_id
                

                然而,当我运行这个查询时,它不给 COUNT(suburb_id) = 0 当郊区_id = 0因为在郊区表中,没有郊区 ID 4,我希望此查询为郊区 ID = 4 返回 0,例如

                however, while I run this query, it doesn't give COUNT(suburb_id) = 0 when suburb_id = 0 because in suburbs table, there is no suburb_id 4, I want this query to return 0 for suburb_id = 4, like

                ============================
                = total       |   suburb_id
                = 2           |    1
                = 1           |    2
                = 1           |    3
                = 0           |    4
                

                推荐答案

                GROUP BY 需要处理行,因此如果某个类别没有行,您将无法获得计数.将 where 子句视为在将源行组合在一起之前对其进行限制.where 子句未提供分组依据的类别列表.

                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.

                您可以做的是编写一个查询来选择类别(郊区),然后在子查询中进行计数.(我不确定 MySQL 对此的支持是怎样的)

                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)

                类似于:

                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,抱歉)

                这篇关于使用 mysql group by 返回计数 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)

                <i id='WJXtx'><tr id='WJXtx'><dt id='WJXtx'><q id='WJXtx'><span id='WJXtx'><b id='WJXtx'><form id='WJXtx'><ins id='WJXtx'></ins><ul id='WJXtx'></ul><sub id='WJXtx'></sub></form><legend id='WJXtx'></legend><bdo id='WJXtx'><pre id='WJXtx'><center id='WJXtx'></center></pre></bdo></b><th id='WJXtx'></th></span></q></dt></tr></i><div id='WJXtx'><tfoot id='WJXtx'></tfoot><dl id='WJXtx'><fieldset id='WJXtx'></fieldset></dl></div>
                <tfoot id='WJXtx'></tfoot>
                  <legend id='WJXtx'><style id='WJXtx'><dir id='WJXtx'><q id='WJXtx'></q></dir></style></legend>

                    <bdo id='WJXtx'></bdo><ul id='WJXtx'></ul>

                      <small id='WJXtx'></small><noframes id='WJXtx'>

                            <tbody id='WJXtx'></tbody>