MySql JOIN两个表SUM错误的结果来自左表

时间:2014-04-18 05:29:51

标签: mysql sql

我有一张两张桌子而且我已经离开了加入,并希望从两张桌子中获得价值总和 但是第二个表的结果未成功检索(SUM没有正确分组)
表格测试1

id  redeem_count        cost   camp_id
1   2                   500    1
2   3                   1000   1
3   2                   2000   2
4   2                   3000   2
5   2                   1200   3

表格测试2

id  bill_amount     earning test1_id
1   4000            50      1
2   5000            55      3
3   6000            60      4

输出

camp_id redeem  cost    bill    earning
1       5       1500    15000   165
2       4       5000    NULL    NULL
3       2       1200    NULL    NULL

期望的结果

camp_id redeem  cost    bill    earning
1       5       1500    4000    50
2       4       5000    11000   115
3       2       1200    0       0

我执行了以下SQL。

SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM (SELECT  COALESCE(SUM(redeem_count),0) AS redeem, COALESCE(SUM(cost),0) AS cost, id,camp_id FROM test1 GROUP BY camp_id) t1
LEFT JOIN(SELECT COALESCE(SUM(bill_amount),0) AS bill, COALESCE(SUM(earning),0) AS earning, test1_id FROM test2) t2
ON t1.id = t2.test1_id

如何实现期望的结果?你能提供解决方案吗?

3 个答案:

答案 0 :(得分:0)

试试这个:

SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM (SELECT  COALESCE(SUM(redeem_count),0) AS redeem, COALESCE(SUM(cost),0) AS cost, id,camp_id FROM test1 GROUP BY camp_id) t1
LEFT JOIN(SELECT COALESCE(bill_amount) AS bill, COALESCE(earning) AS earning, test1_id FROM test2) t2
ON t1.id = t2.test1_id

<强>更新:1

SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM 
(
SELECT  
COALESCE(SUM(redeem_count),0) AS redeem, 
COALESCE(SUM(cost),0) AS cost, 
id,
camp_id 
FROM test1 
GROUP BY camp_id) t1
LEFT JOIN(
SELECT 
SUM(bill_amount) AS bill, 
SUM(earning) AS earning, test1_id FROM test2 GROUP BY test1_id) t2
ON t1.camp_id= t2.test1_id

我的代码产生的输出如下:

camp_id redeem  cost    bill     earning
1       5       1500    4000     50
2       4       5000    11000    115
3       2       1200    (NULL)   (NULL)

答案 1 :(得分:0)

SELECT
t1.camp_id,
t1.redeem,
t1.cost,
t2.bill,
t2.earning
FROM (SELECT  COALESCE(SUM(redeem_count),0) AS redeem, COALESCE(SUM(cost),0) AS cost, id,camp_id FROM test1 GROUP BY camp_id) t1
LEFT JOIN(SELECT COALESCE(bill_amount) AS bill, COALESCE(earning) AS earning, test1_id FROM test2) t2
ON t1.id = t2.test1_id

答案 2 :(得分:0)

<强> EDITED

创建子查询,就像只想查询所需结果一样,然后加入这些结果。

(查询未经过测试!)

SELECT
    X1.camp_id
    , X1.redeem
    , X1.redeem
    , X2.cost
    , X2.earning
FROM
    (
        SELECT
            SUM(redeem_count) AS redeem,
            SUM(cost) AS cost,
            id,
            camp_id
        FROM
            test1
        GROUP BY
            camp_id
    ) X1
    LEFT JOIN (
        SELECT
            T1.camp_id,
            SUM(T2.bill_amount) AS bill,
            SUM(T2.earning) AS earning
        FROM
            test1 T1
            INNER JOIN test2 T2
                ON T1.id = T2.id
        GROUP BY
            T1.camp_id
    ) X2
        ON X1.camp_id = X2.camp_id