2016-01-20 88 views
6

上的庫存管理系統的工作,我們有如下表:自連接只返回一個記錄

================================================ 
| orders | order_line_items | product_options | 
|--------|-------------------|-----------------| 
| id  | id    | id    | 
| start | order_id   | name   | 
| end | product_option_id |     | 
|  | quantity   |     | 
|  | price    |     | 
|  | event_start  |     | 
|  | event_end   |     | 
================================================ 

我試圖來計算某一特定日期的庫存,所以我需要一個自聯接將order_line_items上的數量與具有相同product_option_id的order_line_items中的其他記錄數量的SUM進行比較,以及事件開始和結束在某個範圍內的位置。

所以,對於一個日期2016年1月20日,我有:

SELECT order_line_items.id, order_line_items.product_option_id, order_line_items.order_id FROM order_line_items 
WHERE order_line_items.event_end_date >= '2016-01-20 04:00:00' 
AND order_line_items.event_start_date <= '2016-01-21 04:00:00' 
AND order_line_items.product_option_id IS NOT NULL; 

以上的回報127行

當我嘗試做一個自我加入,就像這樣:

SELECT 
order_line_items.id, 
order_line_items.product_option_id, 
order_line_items.order_id, 
order_line_items.quantity, 

other_line_items.other_product_option_id, 
other_line_items.other_order_id, 
other_line_items.other_quantity, 
other_line_items.total 

FROM order_line_items 

JOIN (
    SELECT 
     id, 
     product_option_id AS other_product_option_id, 
     order_id AS other_order_id, 
     quantity AS other_quantity, 
     SUM(quantity) total 
    FROM order_line_items 
    WHERE order_line_items.event_end_date >= '2016-01-20 04:00:00' 
    AND order_line_items.event_start_date <= '2016-01-21 04:00:00' 
) other_line_items ON order_line_items.product_option_id = other_line_items.other_product_option_id 

WHERE order_line_items.event_end_date >= '2016-01-20 04:00:00' 
AND order_line_items.event_start_date <= '2016-01-21 04:00:00' 
AND order_line_items.product_option_id IS NOT NULL; 

它只返回1條記錄。正如你在這裏看到的:()有相同的product_option_id有很多記錄,所以這最後一個查詢應該返回很多行

+0

一個建議,你需要重命名第二個表,因爲系統可能會混淆,並且不需要第二個地方,因爲你在子查詢中有一個。 –

+0

謝謝 - 刪除第二個讓我獲得更多記錄的地方,但現在所有返回的記錄都有相同的product_option_id ..奇怪:https:// goo。gl/itL5bF – Laravelian

+0

好吧,在'join'選項中試試這個,'order_id = other_order_id和production_order_id = other_production_order_id' –

回答

2

添加SUM(...)將子查詢變成一行。也許,子查詢需要有其中之一:(我不知道該架構或應用不夠好,說這是有道理的)

GROUP BY (id) 
GROUP BY (product_option_id) 
GROUP BY (order_id) 

(請用更短,更鮮明,別名;由於order_line_itemsother_line_items的長度和相似性,SQL非常難以閱讀。)

1

您是否確實想要獲得以下內容? :

SELECT product_option_id, sum(quantity) 
FROM order_line_items 
WHERE event_end_date >= '2016-01-20 04:00:00' 
AND event_start_date <= '2016-01-21 04:00:00' 
GROUP BY 1 

我不能告訴你爲什麼需要一個自我在這裏加入

0

很難告訴你想要什麼沒有樣品的結果,但是這會給你的每一個訂單的產品選項數量的比較該範圍內的總:

SELECT oli.order_id, 
     oli.product_option_id, 
     oli.quantity, 
     po.total_quantity 
    FROM order_line_items oli 
    JOIN (
    SELECT product_option_id, 
      SUM(quantity) total_quantity 
     FROM order_line_items 
    WHERE event_end >= '2016-01-20 04:00:00' 
     AND event_start <= '2016-01-21 04:00:00' 
    GROUP BY product_option_id 
     ) po 
    ON po.product_option_id = oli.product_option_id 
WHERE oli.event_end >= '2016-01-20 04:00:00' 
    AND oli.event_start <= '2016-01-21 04:00:00' 

如果你能有一個順序的相同product_option的多行,你可能需要調整這個像這樣:

SELECT oli.order_id, 
     oli.product_option_id, 
     SUM(oli.quantity) order_quantity, 
     po.total_quantity 
    FROM order_line_items oli 
    JOIN (
     SELECT product_option_id, 
      SUM(quantity) total_quantity 
     FROM order_line_items 
     WHERE event_end >= '2016-01-20 04:00:00' 
     AND event_start <= '2016-01-21 04:00:00' 
    GROUP BY product_option_id 
     ) po 
     ON po.product_option_id = oli.product_option_id 
    WHERE oli.event_end >= '2016-01-20 04:00:00' 
    AND oli.event_start <= '2016-01-21 04:00:00' 
GROUP BY oli.order_id, 
     oli.product_option_id 
0

根據我的理解,您應該將ordersproduct_options加入到order_line_items表中。

您的查詢應該看起來像這樣,對於特定日期之間的訂單上的項目數量。

SELECT product_options.id, production_options.name, SUM(order_line_items.quantity) 
FROM order_line_items 
LEFT JOIN product_options ON production_options.id=order_line_items.product_option_id 
LEFT JOIN orders ON orders.id=order_line_items.order_id 
WHERE orders.start>='SOME DATETIME' AND orders.end<='SOME DATETIME' 
GROUP BY product_options.id 

而且,只是一個評論,product_options應可能只是被命名爲products。贏得更短的表名稱!

+0

我只是想了解更多關於好的餐桌名稱。我會用'訂單','項目'和'order_item'。 – fiddlestacks