MySQL: SUM() with JOIN returns incorrect values

后端 未结 2 861
逝去的感伤
逝去的感伤 2021-01-06 07:36

I am trying to fetch a SUM() for each user in a table, but MySQL is returning the wrong values.

This is how it should look (http://sqlfiddle.com/#!2/7b988/4/0)

2条回答
  •  借酒劲吻你
    2021-01-06 08:19

    This is a old post , but i think this can help others

    use distinct inside sum

    SELECT 
        blocks.user_id, 
        SUM(distinct payout_history.amount) as amount
    FROM blocks
    LEFT JOIN payout_history
    ON blocks.user_id = payout_history.user_id
    WHERE confirms > 520
    GROUP BY blocks.user_id
    

    Refer this answer by @jerome wagner

    MYSQL sum() for distinct rows

提交回复
热议问题