简体   繁体   中英

MySql JOIN two tables SUM wrong result from left table

I have a two table and I have left join and want sum of value from both table.
But the result from second table not retrieved successfully (SUM not group by properly)
Table test1

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

Table test2

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

Output

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

Desired Result

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

I have execute following 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

How can I achieve desired result ? Would you please provide the solution?

try this:

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

update: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

My code produced output like:

camp_id redeem  cost    bill     earning
1       5       1500    4000     50
2       4       5000    11000    115
3       2       1200    (NULL)   (NULL)
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

EDITED

Create your subquery like when you want to query only the required result, then join those results.

(Query is not tested!)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM