mysql - 将两列的对数计算为总量
问题描述
我有一个我想在其他地方导入的积分日志(电子邮件 - 总积分)。
我正在尝试找出执行以下操作的查询:
计算每个 user_id 的点数,并合并这些 user_id,以免重复。
之后,我需要用另一个表中的相应电子邮件替换 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
解决方案
为了美化您的查询,您可以这样写:
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
推荐阅读
- c# - 发布 XML 返回“405 不支持的媒体类型”
- c# - 使用 SwaggerUI 授权 GitHub OAuth Flow
- reactjs - 我有一个反应 js 页面,里面有 5 个组件。我想通过单击按钮来更改文本内容。我该怎么做?
- python - 修改xml文件中子节点的文本值并保存(使用python)
- plugins - 来自带有列表的 hasmap 的 ConcurrentModificationException
- c# - 覆盖 [JsonConverter(typeof(UsualConverter)] 类属性
- arrays - 如何在打字稿中显示从数组中过滤的对象列表?
- django - 如何让用户在线支付费用
- python - Python - 根据 JSON 中的值从 JSON 中删除重复元素
- c# - 如何在检查器中使用字符串插值?