简体   繁体   English

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

[英]MySQL query to get the total from three tables

Using MySQL, how do I get the total items and total revenue for each manager's team? 使用MySQL,我如何获得每个经理团队的总项目和总收入? Suppose I have these 3 different tables (parent-child-grandchild): 假设我有以下3个不同的表(父子孙):

在此处输入图片说明

Employee1 is under Supervisor1, and they are both under Manager1, and so on, but real data are in random arrangement. Employee1在Supervisor1下,他们都在Manager1下,依此类推,但实际数据是随机排列的。 Color-coded the numbers to visualize which gets added. 用数字对数字进行颜色编码以可视化要添加的数字。

I want my query to output the total items and total revenue of each manager's team like: 我希望我的查询输出每个经理团队的总项目和总收入,例如:

在此处输入图片说明

To easily create the table: 要轻松创建表:

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);

SQL Fiddle SQL小提琴

Utilizing a combination of Nested Subqueries and UNION ALL , you can use the following: 结合使用嵌套子查询UNION 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

Try this: http://sqlfiddle.com/#!9/7a0aef/20 试试这个: 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

A single query with the appropriate joins does the trick... In pseudo code (sorry, I'm typing on my phone) 一个具有适当联接的查询就可以解决问题……使用伪代码(对不起,我在手机上打字)

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

Some adjustments may be needed, but this should definitely point you in your way 可能需要进行一些调整,但这绝对可以为您指明方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM