从第3个关系表中总结数量

时间:2015-02-20 08:13:29

标签: php mysql

我有以下表格,我想获取purchase_order及其order_quantity和每个purchase_order的收货数量总和。我知道如何从单个表中总结数量,但是从多个表中总结数量,这让我很困惑......

mysql> select * from purchase_order;
+-------------------+-------------------------+-------+---------------------+
| purchase_order_id | purchase_order          | cost  | created_on          |
+-------------------+-------------------------+-------+---------------------+
|                 1 | Dell Computer 000001256 | 10000 | 2015-02-19 22:14:52 |
|                 2 | HP Computer 000001256   | 50000 | 2015-02-19 22:14:52 |
+-------------------+-------------------------+-------+---------------------+
2 rows in set (0.00 sec)

mysql> select * from purchase_order_detail;
+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
|                        1 |                 1 |     279 |              100 |
|                        2 |                 1 |     286 |              100 |
|                        3 |                 2 |     279 |              200 |
|                        4 |                 2 |     286 |              300 |
+--------------------------+-------------------+---------+------------------+
4 rows in set (0.00 sec)

mysql> select * from delivery_order;
+-------------------+--------------------------+-------------------+---------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity | recieved_on         |
+-------------------+--------------------------+-------------------+---------------------+
|                 1 |                        1 |                50 | 2015-02-19 22:22:51 |
|                 2 |                        2 |                50 | 2015-02-19 22:24:59 |
|                 3 |                        1 |                50 | 2015-02-19 22:34:14 |
|                 4 |                        3 |                70 | 2015-02-20 11:11:31 |
|                 5 |                        4 |               150 | 2015-02-20 11:11:31 |
|                 6 |                        3 |                90 | 2015-02-20 11:12:20 |
|                 7 |                        4 |               100 | 2015-02-20 11:12:20 |
|                 8 |                        3 |                40 | 2015-02-20 11:12:55 |
|                 9 |                        4 |                50 | 2015-02-20 11:12:55 |
+-------------------+--------------------------+-------------------+---------------------+

到目前为止,我有这个查询,但id不会返回正确的记录..

SELECT po.purchase_order_id, SUM(pod.ordered_quantity) AS Sum_of_ordered_quantity, SUM(dor.recieved_quantity) AS Sum_of_recieved_quantity 
FROM purchase_order AS po
    INNER JOIN purchase_order_detail AS pod ON po.purchase_order_id = pod.purchase_order_id
    INNER JOIN delivery_order AS dor ON dor.purchase_order_detail_id = pod.purchase_order_detail_id
GROUP BY po.purchase_order_id

它返回此,

+-------------------+-------------------------+--------------------------+
| purchase_order_id | Sum_of_ordered_quantity | Sum_of_received_quantity |
+-------------------+-------------------------+--------------------------+
|         1         |          300            |          150             |
|         2         |          1500           |          500             |
+-------------------+-------------------------+--------------------------+

您可以在问题中看到,purchase_order_id 1有200个订购数量和150个收货数量,而purchase_order_id 2有500个ordered_quantity和500个收货数量。

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

    SELECT  po.purchase_order_id, 

    SUM(pod.ordered_quantity) AS Sum_of_ordered_quantity, 

    (SELECT `mySelect`.`desired_sum` FROM 
                        (SELECT B.`purchase_order_id` AS myID, SUM(  `A`.`recieved_quantity` ) AS desired_sum
                         FROM  `delivery_order` AS A
                         LEFT JOIN  `purchase_order_detail` AS B ON A.purchase_order_detail_id = B.purchase_order_detail_id
                         GROUP BY B.`purchase_order_id` ) AS mySelect
                         WHERE `mySelect`.`myID` = `po`.`purchase_order_id`) AS Sum_of_received_quantity
    FROM purchase_order AS po
    INNER JOIN purchase_order_detail AS pod ON po.purchase_order_id = pod.purchase_order_id
    GROUP BY po.purchase_order_id

答案 1 :(得分:2)

在使用具有不同连接条件的多个多对多表进行聚合求和功能时,这是常见的。 一种方法是使用相关子查询来获取聚合值,然后进行连接。

的东西
select
po.purchase_order_id,
pod.Sum_of_ordered_quantity,
do.Sum_of_received_quantity
from purchase_order po
join
(
  select purchase_order_id,sum(ordered_quantity) as Sum_of_ordered_quantity 
  from purchase_order_detail 
  group by purchase_order_id
)pod on pod.purchase_order_id = po.purchase_order_id
join 
(
  select 
  t1.purchase_order_id,
  sum(t2.recieved_quantity) as Sum_of_received_quantity
  from purchase_order_detail t1
  join delivery_order t2 on t1.purchase_order_detail_id = t2.purchase_order_detail_id
  group by t1.purchase_order_id
)do on do.purchase_order_id = po.purchase_order_id  

<强> DEMO