1. <small id='byqMn'></small><noframes id='byqMn'>

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

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

        使用union mysql select查询分组

        Group by with union mysql select query(使用union mysql select查询分组)

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

              <tfoot id='kHzsi'></tfoot>
                <legend id='kHzsi'><style id='kHzsi'><dir id='kHzsi'><q id='kHzsi'></q></dir></style></legend>

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

                    <tbody id='kHzsi'></tbody>
                • 本文介绍了使用union mysql select查询分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  (SELECT COUNT(motorbike.`owner_id`) as count,owner.`name`,transport.`type` FROM transport,owner,motorbike WHERE transport.type='motobike'
                  AND owner.`owner_id`=motorbike.`owner_id`
                  AND transport.`type_id`=motorbike.`motorbike_id` GROUP BY motorbike.owner_id)
                  UNION ALL
                  (SELECT COUNT(car.`owner_id`) as count,owner.`name`,transport.`type` FROM transport,owner,car WHERE transport.type='car'
                  AND owner.`owner_id`=car.`owner_id`
                  AND transport.`type_id`=car.`car_id` GROUP BY car.`owner_id`)
                  

                  上面的查询返回如下结果,

                  The query above returns a result like this below,

                  count          name
                  1              Linda
                  2              Mary
                  1              Steve
                  1              Linda
                  

                  此查询用于计算所有者拥有的交通工具数量.琳达有一辆汽车和一辆摩托车,所以结果应该是:

                  This query is to count how many transport that owned by an owner. Linda have one car and one motorcycle,so the result should:

                  count          name
                  2              Linda
                  2              Mary
                  1              Steve
                  

                  我试过这个查询,但返回错误:

                  I have tried this query,but return error:

                  (SELECT COUNT(motorbike.`owner_id`),owner.`name`,transport.`type` FROM transport,owner,motorbike WHERE transport.type='motobike'
                  AND owner.`owner_id`=motorbike.`owner_id`
                  AND transport.`type_id`=motorbike.`motorbike_id`)
                  UNION ALL
                  (SELECT COUNT(car.`owner_id`),owner.`name`,transport.`type` FROM transport,owner,car WHERE transport.type='car'
                  AND owner.`owner_id`=car.`owner_id`
                  AND transport.`type_id`=car.`car_id`)  GROUP BY motorbike.owner_id
                  

                  有人可以帮我吗?

                  推荐答案

                  select sum(qty), name
                  from (
                      select count(m.owner_id) as qty, o.name
                      from transport t,owner o,motorbike m
                      where t.type='motobike' and o.owner_id=m.owner_id
                          and t.type_id=m.motorbike_id
                      group by m.owner_id
                  
                      union all
                  
                      select count(c.owner_id) as qty, o.name,
                      from transport t,owner o,car c
                      where t.type='car' and o.owner_id=c.owner_id and t.type_id=c.car_id
                      group by c.owner_id
                  ) t
                  group by name
                  

                  这篇关于使用union mysql select查询分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 行为不同)
                • <small id='MIY1a'></small><noframes id='MIY1a'>

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

                        <tfoot id='MIY1a'></tfoot><legend id='MIY1a'><style id='MIY1a'><dir id='MIY1a'><q id='MIY1a'></q></dir></style></legend>