<legend id='4Y63p'><style id='4Y63p'><dir id='4Y63p'><q id='4Y63p'></q></dir></style></legend>

    <bdo id='4Y63p'></bdo><ul id='4Y63p'></ul>

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

    2. <small id='4Y63p'></small><noframes id='4Y63p'>

    3. <tfoot id='4Y63p'></tfoot>

      根据条件连接两个表以及第二个表中的记录数

      join two tables along with count of records from second table based on condition(根据条件连接两个表以及第二个表中的记录数)
    4. <i id='qFTPL'><tr id='qFTPL'><dt id='qFTPL'><q id='qFTPL'><span id='qFTPL'><b id='qFTPL'><form id='qFTPL'><ins id='qFTPL'></ins><ul id='qFTPL'></ul><sub id='qFTPL'></sub></form><legend id='qFTPL'></legend><bdo id='qFTPL'><pre id='qFTPL'><center id='qFTPL'></center></pre></bdo></b><th id='qFTPL'></th></span></q></dt></tr></i><div id='qFTPL'><tfoot id='qFTPL'></tfoot><dl id='qFTPL'><fieldset id='qFTPL'></fieldset></dl></div>
    5. <small id='qFTPL'></small><noframes id='qFTPL'>

    6. <legend id='qFTPL'><style id='qFTPL'><dir id='qFTPL'><q id='qFTPL'></q></dir></style></legend>
      <tfoot id='qFTPL'></tfoot>

              <tbody id='qFTPL'></tbody>

                <bdo id='qFTPL'></bdo><ul id='qFTPL'></ul>
                本文介绍了根据条件连接两个表以及第二个表中的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下表格 -

                这里是SQLFIDDLE

                类别

                +-----------+-------------+
                |  column   |    type     |
                +-----------+-------------+
                | id        | int(11)     |
                | name      | varchar(40) |
                | unit      | varchar(50) |
                | is_active | tinyint(1)  |
                +-----------+-------------+
                

                产品

                +-------------+---------------+
                |   column    |     type      |
                +-------------+---------------+
                | id          | int(11)       |
                | category_id | int(11)       |
                | name        | varchar(40)   |
                | base_rate   | decimal(10,2) |
                | is_active   | tinyint(1)    |
                +-------------+---------------+
                

                我想获取类别列表以及活动产品的数量.如果某个类别没有有效的产品,则应返回 0.

                I want to get list of categories along with count of number of products active. If no products are active for a category it should return 0.

                有点像下表 -

                +----+--------+--------------+
                | id |  name  | active_count |
                +----+--------+--------------+
                |  1 | Steel  |            1 |
                |  2 | Cement |            2 |
                +----+--------+--------------+
                

                我想出了以下查询 -

                I have come up with following query -

                SELECT c.id, c.name, c.unit, COUNT(p.category_id) as active_count 
                FROM `categories` c 
                JOIN `products` p
                    ON c.id = p.category_id
                WHERE ( p.is_active = 1 )
                GROUP BY p.category_id;
                

                上述查询仅在每个类别中至少有一个产品处于活动状态时有效.如果没有可用的产品,则应将 active_count 返回为 0

                This above query works only when there is at least one product active in each of category. If there are not products available it should return active_count as 0

                我该如何解决这个问题?

                How can I fix this ?

                这里是SQLFIDDLE

                推荐答案

                使用LEFT JOIN代替INNER JOIN:

                SELECT c.id, c.name, c.unit, COUNT(p.category_id) as active_count 
                FROM `categories` c 
                LEFT JOIN `products` p
                    ON c.id = p.category_id AND p.is_active = 1 
                GROUP BY c.id;
                

                将谓词 p.is_active = 1WHERE 子句移动到 ON 也很重要,这样 allcategories表的em>条记录.

                It is also important to move predicate p.is_active = 1 from WHERE clause to ON, so that all records of categories table are returned by the query.

                这篇关于根据条件连接两个表以及第二个表中的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                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 查询中包含缺失的月份)
                <i id='bIevB'><tr id='bIevB'><dt id='bIevB'><q id='bIevB'><span id='bIevB'><b id='bIevB'><form id='bIevB'><ins id='bIevB'></ins><ul id='bIevB'></ul><sub id='bIevB'></sub></form><legend id='bIevB'></legend><bdo id='bIevB'><pre id='bIevB'><center id='bIevB'></center></pre></bdo></b><th id='bIevB'></th></span></q></dt></tr></i><div id='bIevB'><tfoot id='bIevB'></tfoot><dl id='bIevB'><fieldset id='bIevB'></fieldset></dl></div>

                  <bdo id='bIevB'></bdo><ul id='bIevB'></ul>
                      <tbody id='bIevB'></tbody>
                      <legend id='bIevB'><style id='bIevB'><dir id='bIevB'><q id='bIevB'></q></dir></style></legend>
                      <tfoot id='bIevB'></tfoot>

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