【问题标题】:mysql left join duplicate my table rows - why?mysql left join 重复我的表行 - 为什么?
【发布时间】:2011-05-29 12:35:10
【问题描述】:

我有 2 张桌子:

  • Table1 => product_id, product_quantity => 这里我有 25 行。
  • Table2 => product_hashid, order_quantity => 这里我有 1 个 query 行。

我构建了这个 mysql 查询:

SELECT SUM(table1.product_quantity) - SUM(order_quantity) AS instocks
  FROM table1  -- table.1 in original
  LEFT JOIN table2 ON table2.product_hashid = table1.product_id
 WHERE table1.product_id = '$thisid'

此查询将 table2 行与 table1 重复。这个查询有错误吗?

首先,我想将table1 中的所有product_quantity 相加,其中product_id = '$this'table2 中的所有order_quantity 相加,其中product_hashid = '$this' 并生成(a - b) 以显示最终结果。

【问题讨论】:

  • FROM table.1 是不是抄错了?

标签: mysql sql


【解决方案1】:

你想做什么的大纲很好,但不是你实现的。

  • 我想将所有product_quantitytable1 相加,其中product_id = '$this' 和所有order_quantitytable2 相加,其中product_hashid = '$this' 并生成(a - b) 以显示最终结果。

一步一步构建它。

SELECT SUM(product_quantity) FROM Table1 WHERE Product_ID = '$this';

SELECT SUM(order_quantity) FROM Table2 WHERE Product_HashID = '$this';

SELECT (SELECT SUM(product_quantity) FROM Table1 WHERE Product_ID     = '$this') -
       (SELECT SUM(order_quantity)   FROM Table2 WHERE Product_HashID = '$this')
  FROM Dual;

您可能会观察到代码对齐强​​调了产品 ID 列的不一致列命名。


在更一般的情况下:

SELECT Product_ID, SUM(product_quantity) AS Product_Quantity
  FROM Table1
 GROUP BY Product_ID;

SELECT Product_HashID AS Product_ID, SUM(order_quantity) AS Order_Quantity
  FROM Table2
 GROUP BY Product_HashID;

SELECT p.Product_ID, p.Product_Quantity - o.Order_Quantity AS SurplusOnHand
  FROM (SELECT Product_ID, SUM(product_quantity) AS Product_Quantity
          FROM Table1
         GROUP BY Product_ID) AS P
  JOIN (SELECT Product_HashID AS Product_ID, SUM(order_quantity) AS Order_Quantity
          FROM Table2
         GROUP BY Product_HashID) AS O
    ON O.Product_ID = P.Product_ID;

有时您需要使用 LEFT OUTER JOIN;大多数情况下,你没有。假设你没有写你的 SQL,直到你确定你这样做。

鉴于数据基数(行数),您可能需要在此处执行 LOJ。表1中未列在表2中的产品的订货量需要制造为零。

SELECT    (SELECT SUM(product_quantity) FROM Table1 WHERE Product_ID     = '$this') -
       NVL(SELECT SUM(order_quantity)   FROM Table2 WHERE Product_HashID = '$this'), 0)
  FROM Dual;


SELECT p.Product_ID, p.Product_Quantity - NVL(o.Order_Quantity, 0) AS SurplusOnHand
  FROM (SELECT Product_ID, SUM(product_quantity) AS Product_Quantity
          FROM Table1
         GROUP BY Product_ID) AS P
  LEFT OUTER JOIN
       (SELECT Product_HashID AS Product_ID, SUM(order_quantity) AS Order_Quantity
          FROM Table2
         GROUP BY Product_HashID) AS O
    ON O.Product_ID = P.Product_ID;

【讨论】:

    【解决方案2】:

    LEFT JOIN 在每种情况下都会返回 table1 的所有内容。 http://www.w3schools.com/sql/sql_join_left.asp

    如果它们出现在表 2 中,您真的只想要表 1 中的产品吗?如果是这样,您只需要 JOIN(或 INNER JOIN) http://www.w3schools.com/sql/sql_join_inner.asp

    【讨论】:

      【解决方案3】:

      我认为问题在于您的 JOIN 和 WHERE 子句。在此设置中,您将返回 table2 中的所有数据,这会给您带来混乱。您的 WHERE 子句正在查看 table1 并限制那里的行,但您的 JOIN 返回 table2 中的所有行,并且只返回 table1 中相等的那些行。

      底线:将您的联接更改为 INNER JOIN,您的问题就会消失。

      【讨论】:

      • 首先我想对 table1 中的所有 product_quantity 求和,其中 product_id = '$this' 和 table2 中的所有 order_quantity 求和,其中 product_hashid = '$this' 并制作 (a - b) 以显示最终结果
      • @user764122 - 为此,只需按照我的建议将您的 JOIN 类型更改为 INNER JOIN。这会给你你想要的。
      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2018-12-15
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多