使用sum函数时,我的查询返回单个记录

时间:2015-02-24 07:03:19

标签: php mysql sql

我有以下表结构,关系是一个purchase_order有很多purchase_order_detail,一个purchase_order_detail有很多delivery_order; 的 PURCHASE_ORDER

+-------------------+-------------------------+
| purchase_order_id | purchase_order          |
+-------------------+-------------------------+
|                29 | Dell Computer 000001256 |
|                31 | Dell Computer 000001257 |
+-------------------+-------------------------+

purchase_order_detail

+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
|                       26 |                29 |     279 |              100 |
|                       27 |                29 |     286 |              100 |
|                       28 |                29 |     287 |              100 |
|                       29 |                31 |     279 |               75 |
|                       30 |                31 |     286 |               85 |
+--------------------------+-------------------+---------+------------------+

delivery_order

+-------------------+--------------------------+-------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity |
+-------------------+--------------------------+-------------------+
|                30 |                       26 |                50 |
|                31 |                       27 |                50 |
|                32 |                       28 |                50 |
|                33 |                       29 |                25 |
|                34 |                       30 |                35 |
+-------------------+--------------------------+-------------------+

我想获取purchase_order_detail表中item_id的purchase_order和总收货数量, 我写这个查询,但它返回不正确的记录

SELECT po.created_on, po.purchase_order_id, po.purchase_order, i.item_name, SUM( do.recieved_quantity ) AS quantity
FROM purchase_order po, purchase_order_detail pod, delivery_order do , item i
WHERE i.item_id = pod.item_id
AND po.purchase_order_id = pod.purchase_order_id
AND pod.purchase_order_detail_id = do.purchase_order_detail_id
AND pod.item_id =286

+---------------------+-------------------+-------------------------+-----------+----------+
| created_on          | purchase_order_id | purchase_order          | item_name | quantity |
+---------------------+-------------------+-------------------------+-----------+----------+
| 2015-02-23 13:49:40 |                29 | Dell Computer 000001256 | Lenovo    |       85 |
+---------------------+-------------------+-------------------------+-----------+----------+

您可以看到item_id 286针对两个purchase_orders。

1 个答案:

答案 0 :(得分:3)

你错过了group by条款:

SELECT po.created_on, po.purchase_order_id, po.purchase_order, i.item_name, SUM( do.recieved_quantity ) AS quantity
FROM purchase_order po, purchase_order_detail pod, delivery_order do , item i
WHERE i.item_id = pod.item_id
AND po.purchase_order_id = pod.purchase_order_id
AND pod.purchase_order_detail_id = do.purchase_order_detail_id
AND pod.item_id =286
group by po.created_on, po.purchase_order_id, po.purchase_order, i.item_name