MySQL查询从三个表中获取总数

时间:2018-09-07 14:19:04

标签: mysql

使用MySQL,我如何获取每个经理团队的项目总数和总收入?假设我有以下3个不同的表(parent-child-grandchild):

enter image description here

Employee1在Supervisor1下,并且它们都在Manager1下,依此类推,但是实际数据是随机排列的。对数字进行颜色编码以可视化要添加的数字。

我希望我的查询输出每个经理团队的总项目和总收入,例如:

enter image description here

要轻松创建表,请执行以下操作:

DROP TABLE IF EXISTS manager;
CREATE TABLE manager (id int, name varchar(55), no_of_items int, revenue int);
INSERT INTO manager (id, name, no_of_items, revenue)
VALUES
    (1  , 'Manager1' ,    10    ,   100), 
    (2  , 'Manager2' ,    20    ,   200),
    (3  , 'Manager3' ,    30    ,   300);

DROP TABLE IF EXISTS supervisor;
CREATE TABLE supervisor (id int, name varchar(55), manager_id int, no_of_items int, revenue int);
INSERT INTO supervisor (id, name, manager_id, no_of_items, revenue)
VALUES
    (4  , 'Sup1' ,    1,   100    ,   1000), 
    (5  , 'Sup2' ,    2,   200    ,   2000),
    (6  , 'Sup3' ,    3,   300    ,   3000);

DROP TABLE IF EXISTS employee;
CREATE TABLE employee (id int, name varchar(55), supervisor_id int, no_of_items int, revenue int);
INSERT INTO employee (id, name, supervisor_id, no_of_items, revenue)
VALUES
    (7  , 'Emp1' ,    4,   400    ,   4000), 
    (8  , 'Emp2' ,    5,   500    ,   5000),
    (9  , 'Emp3' ,    4,   600    ,   6000);

3 个答案:

答案 0 :(得分:1)

SQL Fiddle

结合使用Nested SubqueriesUNION ALL,可以使用以下内容:

SELECT inner_nest.manager_id, 
   inner_nest.name, 
   SUM(inner_nest.total_items) AS total_items,  
   SUM(inner_nest.total_revenue) AS total_revenue 

FROM (
  SELECT id as manager_id, 
         name, 
         SUM(no_of_items) AS total_items, 
         SUM(revenue) AS total_revenue 
  FROM manager
  GROUP BY id  

  UNION ALL 

  SELECT m.id as manager_id, 
         m.name, 
         SUM(s.no_of_items) AS total_items, 
         SUM(s.revenue) AS total_revenue 
  FROM manager m
  INNER JOIN supervisor s ON s.manager_id = m.id 
  GROUP BY m.id  

  UNION ALL 

  SELECT m.id as manager_id, 
         m.name, 
         SUM(e.no_of_items) AS total_items, 
         SUM(e.revenue) AS total_revenue 
  FROM manager m
  INNER JOIN supervisor s ON s.manager_id = m.id 
  INNER JOIN employee e ON e.supervisor_id = s.id 
  GROUP BY m.id  
) AS inner_nest 

GROUP BY inner_nest.manager_id

答案 1 :(得分:1)

尝试一下:http://sqlfiddle.com/#!9/7a0aef/20

select id,name,COALESCE(sum(distinct m),0)+COALESCE(sum(distinct s),0)+COALESCE(sum(e),0)
as total_item,COALESCE(sum(distinct mv),0)+COALESCE(sum(distinct sv),0)+COALESCE(sum(ev),0)
as total_revenue
from
(
select m.id,m.name,m.no_of_items as m,s.no_of_items as s,e.no_of_items as e,
  m.revenue as mv,s.revenue as sv,e.revenue as ev
from manager m left join supervisor s
on m.id=s.manager_id
left join employee e on s.id=e.supervisor_id)a
group by id,name

答案 2 :(得分:0)

具有适当联接的单个查询就可以解决问题...用伪代码(抱歉,我在手机上打字)

Select a.manager_id, (a.total_items+b.total_items+c.total_items) as totalitems, (a.total_revenue+b.total_revenue+c.total_revenue) as totalrevenue
From parent_table a
Join child_table b on a.manager_id=b.manager.id
Join grandchild_table c on b.sup_id=c.sup_id
Group by a.manager_id

可能需要进行一些调整,但这绝对可以为您指明方向