自我加入仅返回一条记录

时间:2016-01-20 22:58:38

标签: mysql join

使用库存管理系统,我们有以下表格:

================================================
| 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上的数量与order_line_items中其他记录数量的SUM进行比较product_option_id,以及事件开始和结束的范围。

所以,鉴于2016-01-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条记录。正如你在这里看到的那样:(https://goo.gl/BhUYxK)有很多记录使用相同的product_option_id,所以最后一个查询应该返回很多行

4 个答案:

答案 0 :(得分:2)

添加的SUM(...)将子查询转换为单行。也许子查询需要其中一个:

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

(我不太了解架构或应用程序,无法说出哪些有意义。)

(请使用更短,更独特的别名;由于order_line_itemsother_line_items的长度和相似性,SQL很难阅读。)

答案 1 :(得分: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

我不明白你为什么需要自我加入

答案 2 :(得分: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

答案 3 :(得分: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。胜利的表名更短!