how to get SUM from two different tables that are linked to third one

后端 未结 6 1365
故里飘歌
故里飘歌 2021-01-24 02:41

ok the problem is that I have to sum two sums from two tables that are linked

first table points:

id | user_id | point | hit
1  | 1       |     4 | yes
2         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 02:55

    Small modification to ur code...assuming the id in both fields are not relevant...and ur trying to select user_id and not id

    SELECT p.user_id, SUM(p.point) + SUM(e.earning) AS bb 
    FROM points p JOIN earnings e ON p.user_id = e.user_id 
    WHERE (p.hit = "yes" OR p.hit = "no") 
    GROUP BY p.user_id
    

提交回复
热议问题