查询从四个表中获取数据

时间:2015-02-24 20:18:35

标签: mysql

我有以下方案, 的 PURCHASE_ORDER

+-------------------+----------------------+
| purchase_order_id | purchase_order       |
+-------------------+----------------------+
|                54 | Purchase Order 12345 |
|                56 | po-laptop-hp-3       |
|                57 | po-laptop-hp-1       |
+-------------------+----------------------+

purchase_order_detail

+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
|                       61 |                54 |     279 |              500 |
|                       62 |                54 |     286 |              700 |
|                       63 |                56 |     279 |               43 |
|                       64 |                57 |     279 |               43 |
|                       65 |                57 |     286 |               43 |
|                       66 |                57 |     287 |               43 |
+--------------------------+-------------------+---------+------------------+

delivery_order

+-------------------+--------------------------+-------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity |
+-------------------+--------------------------+-------------------+
|                62 |                       61 |               250 |
|                63 |                       62 |               300 |
|                64 |                       63 |                34 |
|                65 |                       64 |                34 |
|                66 |                       65 |                34 |
|                67 |                       66 |                34 |
|                68 |                       61 |                34 |
|                69 |                       61 |                34 |
+-------------------+--------------------------+-------------------+

股票<​​/强>

+----------+-------------------+------------+----------+------------------+---------------+
| stock_id | delivery_order_id | project_id | quantity | initial_quantity | stock_type_id |
+----------+-------------------+------------+----------+------------------+---------------+
|       12 |                62 |          1 |       60 |               60 |             1 |
|       13 |                63 |          1 |      120 |              120 |             1 |
|       14 |                63 |          1 |       50 |               50 |             1 |
|       15 |                64 |          1 |       12 |               12 |             1 |
|       16 |                62 |          1 |      120 |              120 |             1 |
|       17 |                62 |          1 |       12 |               12 |             1 |
+----------+-------------------+------------+----------+------------------+---------------+

我已经写了这个查询,但它返回了重复的结果

      SELECT po.created_on
           , po.purchase_order
           , i.item_name
           , u.unit_name
           , pod.ordered_quantity
           , do.recieved_quantity
           , do.recieved_on
           , po.remarks
        FROM purchase_order        po
           , purchase_order_detail pod
           , delivery_order        do
           , stock                 s
           , item                  i
           , unit                  u
       WHERE u.unit_id                    = i.unit_id 
         AND 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 do.delivery_order_id         = s.delivery_order_id
         AND s.project_id                 = 1 
    ORDER BY po.purchase_order_id
           , pod.item_id
           ;

结果

+---------------------+----------------------+------------+-----------+------------------+-------------------+---------------------+---------------------------------------+
| created_on          | purchase_order       | item_name  | unit_name | ordered_quantity | recieved_quantity | recieved_on         | remarks                               |
+---------------------+----------------------+------------+-----------+------------------+-------------------+---------------------+---------------------------------------+
| 2015-02-24 22:48:15 | Purchase Order 12345 | HP Laptops | Unit      |              500 |               250 | 2015-02-21 00:00:00 | Adding first Purchase Order as a Test |
| 2015-02-24 22:48:15 | Purchase Order 12345 | HP Laptops | Unit      |              500 |               250 | 2015-02-21 00:00:00 | Adding first Purchase Order as a Test |
| 2015-02-24 22:48:15 | Purchase Order 12345 | Lenovo     | Unit      |              700 |               300 | 2015-02-21 00:00:00 | Adding first Purchase Order as a Test |
| 2015-02-24 22:48:15 | Purchase Order 12345 | Lenovo     | Unit      |              700 |               300 | 2015-02-21 00:00:00 | Adding first Purchase Order as a Test |
| 2015-02-24 22:55:40 | po-laptop-hp-3       | HP Laptops | Unit      |               43 |                34 | 2015-02-21 00:00:00 | dfgsdfgsd                             |
+---------------------+----------------------+------------+-----------+------------------+-------------------+---------------------+---------------------------------------+

关系从上到下是一对多。 我想得到的是每个purchase_order,每个项目的订购数量,总收货数量,以及库存中project_id = 1的库存数量。

我期待这样的事情,

+-------------------+---------+------------------+---------------+----------+
| purchase_order_id | item_id | ordered_quantity | totalReceived | quantity |
+-------------------+---------+------------------+---------------+----------+
|                54 |     279 |              500 |           314 |      192 |
|                54 |     286 |              700 |           300 |      170 |
|                56 |     279 |               43 |            34 |       12 |
+-------------------+---------+------------------+---------------+----------+

2 个答案:

答案 0 :(得分:2)

修改

感谢您在我的第一部分中清除错误。我现在意识到我们不能在一个查询中进行所有计算(因为我们将各个部分放在不同的列上)所以我开始编写单个子查询并将它们连接在一起。步骤是这样的:

  • 获取每个收到的总收到数量的总和 来自delivery_order表的 purchase_order_detail_id
  • 使用delivery_order表本身加入该子查询,以获取各种 delivery_order_id 值的totalReceived。
  • 使用purchase_order_detail表加入该结果集,以获取每个 delivery_order_id 的purchase_order_id,item_id和ordered_quantity。

我们现在有一个结果集,包括delivery_order_id,purchase_order_id,item_id,ordered_quantity和收到的总数。最后两件事是:

  • 从库存表中获取每个delivery_order_id的数量的SUM()。
  • 在order_id匹配的情况下加入我们的上述结果集(因此我们只得到一行)并且project_id为1(所以我们只得到必要的delivery_order_id值)。我将该条件放在sum子查询的WHERE子句中。

这是您的最终查询:

SELECT tmp1.purchase_order_id, tmp1.item_id, tmp1.ordered_quantity, tmp1.totalReceived, tmp2.quantity
FROM(
  SELECT tmp.delivery_order_id, pod.purchase_order_id, pod.item_id, pod.ordered_quantity, tmp.totalReceived
  FROM purchase_order_detail pod
  JOIN(
    SELECT do.delivery_order_id, tmp.purchase_order_detail_id, tmp.totalReceived
    FROM delivery_order do
    JOIN(
      SELECT do.purchase_order_detail_id, SUM(do.received_quantity) AS totalReceived
      FROM delivery_order do
      GROUP BY do.purchase_order_detail_id) tmp ON tmp.purchase_order_detail_id = do.purchase_order_detail_id) 
    tmp ON tmp.purchase_order_detail_id = pod.purchase_order_detail_id) tmp1
JOIN(
  SELECT s.delivery_order_id, SUM(quantity) AS quantity
  FROM stock s
  WHERE s.project_id = 1
  GROUP BY s.delivery_order_id) tmp2 ON tmp2.delivery_order_id = tmp1.delivery_order_id;

这是SQL Fiddle。它还显示了所有中间步骤,如果您想看看结果如何单独组合在一起。

答案 1 :(得分:0)

尝试修改您的查询以使用DISTINCT和OUTER JOIN而不是笛卡尔(&#34;逗号&#34;)连接。

  SELECT DISTINCT po.created_on
       , po.purchase_order
       , i.item_name
       , u.unit_name
       , pod.ordered_quantity
       , do.recieved_quantity
       , do.recieved_on
       , po.remarks
    FROM purchase_order po
    LEFT JOIN purchase_order_detail pod USING (purchase_order_id)
    LEFT JOIN delivery_order do USING (purchase_order_detail_id)
    LEFT JOIN stock s USING (delivery_order_id)
    LEFT JOIN item i USING (item_id)
    LEFT JOIN unit u USING (unit_id)
ORDER BY po.purchase_order_id
       , pod.item_id
       ;