首页 > 解决方案 > 将两列的对数计算为总量

问题描述

我有一个我想在其他地方导入的积分日志(电子邮件 - 总积分)。

我正在尝试找出执行以下操作的查询:

  1. 计算每个 user_id 的点数,并合并这些 user_id,以免重复。

  2. 之后,我需要用另一个表中的相应电子邮件替换 USER_ID:

源表

USER_ID        Points
-------        ------

1                10
2                30
3                50
1                -5
2                 5
3                -40

期望的结果

USER_ID        Points
-------        ------

1                5
2                35
3                10

第2步

另一个源表

USER_ID        Email
-------        ------

1                one@one.com
2                two@two.com
3                three@three.com

最终预期结果:

USER_ID                Total Points
-------                -----------

one@one.com              5
two@two.com              35
three@three.com          10

标签: mysqlphpmyadmin

解决方案


为了美化您的查询,您可以这样写:

select oc_customer.email,
sum(oc_customer_reward.points) as Total_points
from oc_customer_reward
inner join oc_customer using(customer_id)
group by customer_id

推荐阅读