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
.
我有一个mysql表名支付(包含客户支付)和另一个名为lead(包含客户)的表。当客户付款时,它将逐行添加到具有客户ID的付款表中。我想使用customer_id获取特定客户的付款金额总和。
How can I do it with mysql SUM function ?
我怎么能用mysql SUM函数做到这一点?
3 个解决方案
#1
1
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.
在这种情况下不需要显式的GROUP BY,因为mysql会自动采用正确的方法。可能需要更复杂的查询。首先阅读这里的手册。
#2
1
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
#3
1
select sum(paid_amount) from payments where customer_id = x
#1
1
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.
在这种情况下不需要显式的GROUP BY,因为mysql会自动采用正确的方法。可能需要更复杂的查询。首先阅读这里的手册。
#2
1
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
#3
1
select sum(paid_amount) from payments where customer_id = x