在sql中使用join和group by添加两个sum

gpnt7bae  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(448)
SELECT t1.id, t1.name,SUM(t3.points), SUM(t2.points)
FROM app_users t1
LEFT JOIN loyalty_points_redeem t3 ON t3.app_user_id = t1.id
LEFT JOIN loyalty_points_gain t2 ON t2.app_user_id = t1.id    
GROUP BY t1.id

我想从表中计算一个特殊i的和第一个和是可以的,但另一个是不正确的,我是新的在sql中如何解决,如果有任何想法请帮助

p8h8hvxi

p8h8hvxi1#

看不到其他组的问题

SELECT t1.id, t1.name,SUM(t3.points), SUM(t2.points)
FROM app_users t1
LEFT JOIN loyalty_points_redeem t3 ON t3.app_user_id = t1.id
LEFT JOIN loyalty_points_gain t2 ON t2.app_user_id = t1.id    
GROUP BY t1.id, t1.name
yhxst69z

yhxst69z2#

您可以通过在GROUPBY子句中添加t1.name来尝试以下操作

SELECT t1.id, t1.name,SUM(t3.points), SUM(t2.points)
FROM app_users t1
LEFT JOIN loyalty_points_redeem t3 ON t3.app_user_id = t1.id
LEFT JOIN loyalty_points_gain t2 ON t2.app_user_id = t1.id    
GROUP BY t1.id,t1.name

相关问题