Bala
Bala

Reputation: 3638

how to get the sum of values in the different field in mysql?

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 ?

Upvotes: 1

Views: 186

Answers (3)

Michael Durrant
Michael Durrant

Reputation: 96454

select sum(paid_amount) from payments where customer_id = x

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359776

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

Upvotes: 1

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656241

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.

Upvotes: 1

Related Questions