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

      时间:2024-04-16
        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 Group By 和 min (MySQL)

                相关文章

                <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'>