如何获得mysql中不同字段的值的总和?
我有一个名为 payment 的 mysql 表(包含客户付款)和另一个名为
Lead 的表(包含客户)。当客户付款时,它将与客户 ID 一起逐行添加到
payment 表中。我想使用
customer_id
获取特定客户的支付金额总和。
我该如何使用 mysql SUM 函数来做到这一点?
I have a mysql table name payment
(contains payments made by the customer) and another table called lead
(contains customers). When the customer makes a payment it will be added in the payment
table with customer id row by row. I want to fetch the sum of the paid amount of the particular customer using the customer_id
.
How can I do it with mysql SUM function ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就这么简单:
在这种情况下不需要显式的 GROUP BY,因为 mysql 会自动假设正确的事情。更复杂的查询中可能需要。
首先阅读此处的手册。
As simple as that:
An explicit GROUP BY is not needed in this case, because mysql assumes the right thing automatically. May be needed in a more complex query.
Start by reading the manual here.
要为单个客户选择付款金额:
要为每个客户选择付款金额:
To select the sum of payments for a single customer:
To select the sum for payments for each customer: