<i id='LVLwI'><tr id='LVLwI'><dt id='LVLwI'><q id='LVLwI'><span id='LVLwI'><b id='LVLwI'><form id='LVLwI'><ins id='LVLwI'></ins><ul id='LVLwI'></ul><sub id='LVLwI'></sub></form><legend id='LVLwI'></legend><bdo id='LVLwI'><pre id='LVLwI'><center id='LVLwI'></center></pre></bdo></b><th id='LVLwI'></th></span></q></dt></tr></i><div id='LVLwI'><tfoot id='LVLwI'></tfoot><dl id='LVLwI'><fieldset id='LVLwI'></fieldset></dl></div>
      • <bdo id='LVLwI'></bdo><ul id='LVLwI'></ul>
    1. <tfoot id='LVLwI'></tfoot>
    2. <small id='LVLwI'></small><noframes id='LVLwI'>

        <legend id='LVLwI'><style id='LVLwI'><dir id='LVLwI'><q id='LVLwI'></q></dir></style></legend>

        SQL Query to Count() 多个表

        SQL Query to Count() multiple tables(SQL Query to Count() 多个表)

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

        • <small id='eQ2t4'></small><noframes id='eQ2t4'>

          <tfoot id='eQ2t4'></tfoot>
          • <legend id='eQ2t4'><style id='eQ2t4'><dir id='eQ2t4'><q id='eQ2t4'></q></dir></style></legend>

                <bdo id='eQ2t4'></bdo><ul id='eQ2t4'></ul>
                  本文介绍了SQL Query to Count() 多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个表,它与其他表有几个一对多的关系.假设主表是一个人,其他表代表宠物、汽车和儿童.我想要一个查询,返回此人的详细信息,他们拥有的宠物、汽车和孩子的数量,例如

                  <前>Person.Name Count(cars) Count(children) Count(pets)约翰史密斯 3 2 4鲍勃·布朗 1 3 0

                  最好的方法是什么?

                  解决方案

                  Subquery Factoring (9i+):

                  WITH count_cars AS (选择 t.person_idCOUNT(*) num_cars从汽车 cGROUP BY t.person_id),count_children AS (选择 t.person_idCOUNT(*) num_children从儿童 cGROUP BY t.person_id),count_pets AS (选择 p.person_idCOUNT(*) num_pets来自宠物 pGROUP BY p.person_id)选择 t.name,NVL(cars.num_cars, 0) 'Count(cars)',NVL(children.num_children, 0) 'Count(children)',NVL(pets.num_pets, 0) 'Count(pets)'从人 tLEFT JOIN count_cars 汽车 ONcars.person_id = t.person_idLEFT JOIN count_children 儿童 ON children.person_id = t.person_idLEFT JOIN count_pets pets ON pets.person_id = t.person_id

                  使用内联视图:

                   SELECT t.name,NVL(cars.num_cars, 0) 'Count(cars)',NVL(children.num_children, 0) 'Count(children)',NVL(pets.num_pets, 0) 'Count(pets)'从人 t左连接(选择 t.person_idCOUNT(*) num_cars从汽车 cGROUP BY t.person_id) 汽车 ONcars.person_id = t.person_id左连接(选择 t.person_idCOUNT(*) num_children从儿童 cGROUP BY t.person_id) children ON children.person_id = t.person_id左连接(选择 p.person_idCOUNT(*) num_pets来自宠物 pGROUP BY p.person_id) pets ON pets.person_id = t.person_id

                  I have a table which has several one to many relationships with other tables. Let's say the main table is a person, and the other tables represent pets, cars and children. I would like a query that returns details of the person,the number of pets, cars and children they have e.g.

                  Person.Name   Count(cars) Count(children) Count(pets)
                  
                  John Smith    3           2               4
                  Bob Brown     1           3               0
                  

                  What is the best way to do this?

                  解决方案

                  Subquery Factoring (9i+):

                  WITH count_cars AS (
                      SELECT t.person_id
                             COUNT(*) num_cars
                        FROM CARS c
                    GROUP BY t.person_id),
                       count_children AS (
                      SELECT t.person_id
                             COUNT(*) num_children
                        FROM CHILDREN c
                    GROUP BY t.person_id),
                       count_pets AS (
                      SELECT p.person_id
                             COUNT(*) num_pets
                        FROM PETS p
                    GROUP BY p.person_id)
                     SELECT t.name,
                            NVL(cars.num_cars, 0) 'Count(cars)',
                            NVL(children.num_children, 0) 'Count(children)',
                            NVL(pets.num_pets, 0) 'Count(pets)'
                       FROM PERSONS t
                  LEFT JOIN count_cars cars ON cars.person_id = t.person_id
                  LEFT JOIN count_children children ON children.person_id = t.person_id
                  LEFT JOIN count_pets pets ON pets.person_id = t.person_id
                  

                  Using inline views:

                     SELECT t.name,
                            NVL(cars.num_cars, 0) 'Count(cars)',
                            NVL(children.num_children, 0) 'Count(children)',
                            NVL(pets.num_pets, 0) 'Count(pets)'
                       FROM PERSONS t
                  LEFT JOIN (SELECT t.person_id
                                    COUNT(*) num_cars
                               FROM CARS c
                           GROUP BY t.person_id) cars ON cars.person_id = t.person_id
                  LEFT JOIN (SELECT t.person_id
                                    COUNT(*) num_children
                               FROM CHILDREN c
                           GROUP BY t.person_id) children ON children.person_id = t.person_id
                  LEFT JOIN (SELECT p.person_id
                                    COUNT(*) num_pets
                               FROM PETS p
                           GROUP BY p.person_id) pets ON pets.person_id = t.person_id
                  

                  这篇关于SQL Query to Count() 多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 查询中包含缺失的月份)
                1. <tfoot id='j5lfF'></tfoot>

                        <legend id='j5lfF'><style id='j5lfF'><dir id='j5lfF'><q id='j5lfF'></q></dir></style></legend>

                        • <bdo id='j5lfF'></bdo><ul id='j5lfF'></ul>

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

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