如何在MySQL中的SUM列左连接

时间:2015-06-09 11:41:20

标签: mysql

所以我有表 cont_selling

---------------------------------
cont_selling_id   |     date    | 
---------------------------------
     1            | 2015-05-24  | 
     2            | 2015-06-06  | 
---------------------------------

表02 cont_sold

   ----------------------------------------------------
    cont_sold_id |    cont_selling_id   |     price   | 
    ---------------------------------------------------
      1          |         1            |       10    | 
      2          |         1            |       10    | 
      3          |         1            |       30    | 
      4          |         2            |       20    | 
      5          |         2            |       10    | 
    --------------------------------------------------

和表03 付款

 ----------------------------------------------
payment_id   |  cont_selling_id |      paid   | 
-----------------------------------------------
    1        |         1        |       10    | 
    2        |         2        |       10    | 
    3        |         1        |       20    | 
    4        |         1        |       10    | 
    5        |         2        |       10    | 
-----------------------------------------------

现在我需要基于

的SELECT表

现在我想基于 cont_selling cont_selling_id 列合并所有这三个表

并希望SUM cont_sold表价格列和付款表付费列

这就是我想做的事情

期待输出

---------------------------------------------
cont_selling_id  |    price    |      paid   | 
---------------------------------------------
       1         |      50     |       40    | 
       2         |      30     |       20    | 
---------------------------------------------

所以我在mysql查询中尝试了这样但它给出了错误的总和结果

                    SELECT 

                    SUM(Z.price) as total, 
                    SUM(P.amount) as paid

                    FROM cont_selling S

                    LEFT JOIN cont_sold Z
                    ON S.cont_selling_id = Z.cont_selling_id

                    LEFT JOIN payment P
                    ON S.cont_selling_id = P.cont_selling_id    

                    GROUP BY S.cont_selling_id

对于上面的查询我得到这样的输出

---------------------------------------------
cont_selling_id  |    price    |      paid   | 
---------------------------------------------
       1         |      150    |       40    | 
       2         |      60     |      120    | 
---------------------------------------------

3 个答案:

答案 0 :(得分:2)

这里你可以使用aggegare部分进入内部查询,然后加入

select 
cs.cont_selling_id,
price,
paid
from cont_selling cs
left join(
 select sum(price) as price , cont_selling_id from cont_sold
 group by cont_selling_id
)x on x.cont_selling_id = cs.cont_selling_id,
left join(
 select sum(paid) as paid , cont_selling_id from payment
 group by cont_selling_id
)y
on y.cont_selling_id = cs.cont_selling_id; 

答案 1 :(得分:1)

此未经测试的查询应该有效:

with a as(select  cont_selling_id  , sum(price) as totalprice from cont_sold group by cont_selling_id),
with a as(select  cont_selling_id  , sum(paid) as totalpaid from payment group by cont_selling_id),
select c.cont_selling_id , totalprice, totalpaid from cont_selling c left join a.count_selling_id = c.count_selling_id
left join b.count_selling_id = c.count_selling_id

您必须创建临时表,因为您的表与价格和付费之间没有依赖关系。

答案 2 :(得分:1)

您应该使用SUM进行两个不同的查询,然后将它们组合起来以获得所需的结果:

SELECT T1.cont_selling_id,T1.price,T2.paid
FROM
(SELECT c.cont_selling_id,SUM(cs.price) as price
 FROM cont_selling c LEFT JOIN
     cont_sold cs ON c.cont_selling_id=cs.cont_selling_id
 GROUP BY c.cont_selling_id) as T1 JOIN
(SELECT c.cont_selling_id,SUM(p.paid) as paid
 FROM cont_selling c LEFT JOIN
      payment p ON p.cont_selling_id=c.cont_selling_id
  GROUP BY c.cont_selling_id) as T2 ON T1.cont_selling_id=T2.cont_selling_id

结果:

cont_selling_id price   paid
----------------------------
1               50      40
2               30      20

SQL Fiddle中的示例结果。