如何获得mysql中不同字段的值的总和?

发布于 2024-12-17 15:44:51 字数 255 浏览 0 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最丧也最甜 2024-12-24 15:44:51

就这么简单:

SELECT sum(payment) AS payment_sum
FROM   payment
WHERE  customer_id = <your_id>

在这种情况下不需要显式的 GROUP BY,因为 mysql 会自动假设正确的事情。更复杂的查询中可能需要。
首先阅读此处的手册

As simple as that:

SELECT sum(payment) AS payment_sum
FROM   payment
WHERE  customer_id = <your_id>

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.

倾城月光淡如水﹏ 2024-12-24 15:44:51

要为单个客户选择付款金额:

SELECT SUM(p.amount) FROM payment p WHERE p.customer_id = 42

要为每个客户选择付款金额:

SELECT SUM(p.amount) FROM payment p GROUP BY p.customer_id

To select the sum of payments for a single customer:

SELECT SUM(p.amount) FROM payment p WHERE p.customer_id = 42

To select the sum for payments for each customer:

SELECT SUM(p.amount) FROM payment p GROUP BY p.customer_id
奢华的一滴泪 2024-12-24 15:44:51
select sum(paid_amount) from payments where customer_id = x
select sum(paid_amount) from payments where customer_id = x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文