MySQL:在进行多次连接时,如何对非重复值求和

MySQL:在进行多次连接时,如何对非重复值求和,sql,algorithm,syntax,mysql,Sql,Algorithm,Syntax,Mysql,我尝试从包含一些联接的查询中求列和值 例如: SELECT p.id AS product_id, SUM(out_details.out_details_quantity) AS stock_bought_last_month, SUM(order_details.order_quantity) AS stock_already_commanded FROM product AS p INNER JOIN out_details ON out_details.prod

我尝试从包含一些联接的查询中求列和值

例如:

SELECT
    p.id AS product_id,
    SUM(out_details.out_details_quantity) AS stock_bought_last_month,
    SUM(order_details.order_quantity) AS stock_already_commanded
FROM product AS p 
INNER JOIN out_details ON out_details.product_id=p.id 
INNER JOIN order_details ON order_details.product_id=p.id 
WHERE p.id=9507
GROUP BY out_details.out_details_pk, order_details.id;
我得到这个结果:

+------------+-------------------------+-------------------------+
| product_id | stock_bought_last_month | stock_already_commanded |
+------------+-------------------------+-------------------------+
|       9507 |                      22 |                      15 |
|       9507 |                      22 |                      10 |
|       9507 |                      10 |                      15 |
|       9507 |                      10 |                      10 |
|       9507 |                       5 |                      15 |
|       9507 |                       5 |                      10 |
+------------+-------------------------+-------------------------+
现在,我想求和这些值,但当然有重复的。我还必须按产品编号进行分组:

SELECT 
  p.id AS product_id,
  SUM(out_details.out_details_quantity) AS stock_bought_last_month,
  SUM(order_details.order_quantity) AS stock_already_commanded
FROM product AS p 
INNER JOIN out_details ON out_details.product_id=p.id 
INNER JOIN order_details ON order_details.product_id=p.id 
WHERE p.id=9507
GROUP BY p.id;
结果:

+------------+-------------------------+-------------------------+
| product_id | stock_bought_last_month | stock_already_commanded |
+------------+-------------------------+-------------------------+
|       9507 |                      74 |                      75 |
+------------+-------------------------+-------------------------+
结果是:

+------------+-------------------------+-------------------------+
| product_id | stock_bought_last_month | stock_already_commanded |
+------------+-------------------------+-------------------------+
|       9507 |                      37 |                      25 |
+------------+-------------------------+-------------------------+
如何忽略重复项?当然,行数可以更改

另一种方法:

Select P.Id
    , Coalesce(DetailTotals.Total,0) As stock_bought_last_month
    , Coalesce(OrderTotals.Total,0) As stock_already_commanded
From product As P
    Left Join   (
                Select O1.product_id, Sum(O1.out_details_quantity) As Total
                From out_details As O1
                Group By O1.product_id
                ) As DetailTotals
        On DetailTotals.product_id = P.id
    Left Join   (
                Select O2.product_id, Sum(O2.order_quantity) As Total
                From order_details As O2
                Group By O2.product_id
                ) As OrderTotals
        On OrderTotals.product_id = P.id
Where P.Id = 9507   
SELECT
    p.product_id,
    p.stock_bought_last_month,
    SUM(order_details.order_quantity) AS stock_already_commanded
from
(SELECT
    product.id AS product_id,
    SUM(out_details.out_details_quantity) AS stock_bought_last_month,
FROM product 
INNER JOIN out_details ON out_details.product_id=product.id 
WHERE product.id=9507
group by product.id
) AS p 
INNER JOIN order_details ON order_details.product_id=p.product_id
group by p.product_id;

严格地说,在本例中,group by子句是不必要的,因为只有一个产品id—但是,如果选择了多个产品id,则它们将是必需的。

我认为您必须执行两个子查询。一个是上个月在产品id和库存上购买的,另一个是在产品id和库存上已经购买的。每个的值将总和(存量)除以计数(存量),并使用where筛选出零计数。为什么要按out_详细信息进行分组。out_详细信息_pk?这是否回答了您的问题?就是这样!我不知道在联接项中可以有子查询。谢谢你,这真的很有帮助!