如何计算 MYSQL 中多个表的总和

How to Calculate the Sum of Multiple Tables in MYSQL

我有 3 个表,即:Inventory、ConsumedProducts、DamagedProducts

库存:

|ID|TransactionDate     |Item             |Unit|Quantity|
---------------------------------------------------------
|1 |2019-07-10 12:23:51 |Plastic Cup 22oz |Pc  |200     |
---------------------------------------------------------
|2 |2019-07-10 01:23:51 |Plastic Cup 16oz |Pc  |100     |
---------------------------------------------------------
|3 |2019-07-10 01:23:51 |Plastic Cup 22oz |Pc  |100     |
---------------------------------------------------------
|4 |2019-07-10 01:23:51 |Lemon            |Pc  |100     |
---------------------------------------------------------

消费产品:

|ID|TID|TransactionDate     |Item           |Unit|Quantity|
---------------------------------------------------------

|1 |1  |2019-07-10 12:23:51 |Plastic Cup 22oz |Pc  |1     |
---------------------------------------------------------
|2 |1  |2019-07-10 01:23:51 |Lemon            |Pc  |1     |
---------------------------------------------------------
|3 |2  |2019-07-10 01:23:51 |Plastic Cup 16oz |Pc  |1     |
---------------------------------------------------------
|4 |2  |2019-07-10 01:23:51 |Lemon            |Pc  |1     |
---------------------------------------------------------
|5 |3  |2019-07-10 01:23:51 |Plastic Cup 16oz |Pc  |1     |
---------------------------------------------------------

损坏的产品:

|ID|TransactionDate     |Item             |Unit|Quantity|
---------------------------------------------------------
|1 |2019-07-10 12:23:51 |Plastic Cup 22oz |Pc  |10      |
---------------------------------------------------------
|2 |2019-07-10 01:23:51 |Plastic Cup 16oz |Pc  |10      |
---------------------------------------------------------
|3 |2019-07-10 01:23:51 |Plastic Cup 22oz |Pc  |5       |
---------------------------------------------------------
|4 |2019-07-10 01:23:51 |Lemon            |Pc  |6       |
---------------------------------------------------------

如何制作这样的输出?

库存:

|ID|Item             |Inv Bal|Consumed Prod|Dmgd Prod|Actual Balance
---------------------------------------------------------
|1 |Plastic Cup 22oz |300    |    1        |15       |284
---------------------------------------------------------
|2 |Plastic Cup 16oz |100    |    2        |10       |88
---------------------------------------------------------
|3 |Lemon            |100    |    2        |6        |92
---------------------------------------------------------

我试过其他方法,但没有给我想要的结果。我,只是一个有抱负的程序员,所以任何帮助将不胜感激。

SELECT 
  I.ID, I.Item, IFNULL(SUM(I.Quantity),0), 
  IFNULL(SUM(C.Quantity),0), IFNULL(SUM(D.Quantity),0), 
  IFNULL((IFNULL(SUM(I.Quantity),0) - (IFNULL(SUM(C.Quantity),0) + 
  IFNULL(SUM(D.Quantity),0))),0) AS NEW_BAL 
From 
 Inventory I 
 Left OUTER Join ConsumedProducts C 
 ON I.Item = C.Item 
 LEFT Outer Join DamagedProducts D 
 ON D.Item = I.Item 
GROUP BY I.Item, C.Item, D.Item

输出乘以另一个table的结果。

加入子查询如下:

SELECT 
  I.ID, I.Item, SUM(I.Quantity) as `Inv Bal`,
  D.Quantity as damagedQTY,
  C.Quantity as ConsumedQTY,
  SUM(I.Quantity) + D.Quantity -C.Quantity as NEWBALANCE  
From 
 Inventory I 
 LEFT OUTER JOIN
 ( select item,SUM(Quantity) as Quantity from DamagedProducts group by item) D on I.item=D.item
 LEFT OUTER JOIN
 (select item,SUM(Quantity) as Quantity from ConsumedProducts group by tID) C on I.item=C.item
GROUP BY I.Item

您的问题的解决方案(我认为可能不是优化查询):

SET @row_number = 0;

SELECT 
(@row_number := @row_number+1) ID
,I.ITEM
,I.Quantity INV_BAL
,CP.Quantity CONSUMED_PROD
,DP.Quantity DMGD_PROD
,I.Quantity - CP.Quantity - DP.Quantity AS NEW_BAL 
FROM
(SELECT ITEM, SUM(QUANTITY) QUANTITY FROM Inventory GROUP BY ITEM) I
LEFT JOIN 
(SELECT ITEM, SUM(QUANTITY) QUANTITY FROM ConsumedProducts GROUP BY ITEM) CP
ON I.ITEM = CP.ITEM
LEFT JOIN 
(SELECT ITEM, SUM(QUANTITY) QUANTITY FROM DamagedProducts GROUP BY ITEM) DP
ON I.ITEM = DP.ITEM
ORDER BY I.ITEM DESC

输出:

ID  ITEM                INV_BAL CONSUMED_PROD   DMGD_PROD   NEW_BAL
1   Plastic Cup 22oz    300     1               15          284
2   Plastic Cup 16oz    100     2               10           88
3   Lemon               100     2                6           92

演示Link到解决方案:

http://sqlfiddle.com/#!9/684a8b/11