tags:

views:

55

answers:

5

Hi there,

I have a table in mysql which holds the values in integers, my table is something like this.

alt text

I want to sum up the values of a particular column, for example i want to calculate the total amount, total cashpaid and total balance. hwo do i do it?

+4  A: 

Use SUM() function of MySQL like this:

select SUM(amount) from tablename;
select SUM(cashpaid) from tablename;
select SUM(balance) from tablename;

OR you can group them into one:

select SUM(amount), SUM(cashpaid), SUM(balance) from tablename;
shamittomar
thank you shamit. :) works perfectly fine.
Ibrahim Azhar Armar
You're welcome. You can also put them into a single query.
shamittomar
i already did it and put it in single query. :)
Ibrahim Azhar Armar
A: 

Try out this:

select SUM(amount) AS total_amount, SUM(cashpaid) AS total_cashpaid, SUM(balance) AS total_balance  from tablename;
boss
+1  A: 

if you want to do it in one single query:

SELECT SUM(amount) as total_amount, SUM(cashpaid) as total_paid,SUM(balance) as total_balance FROM tablename;

to count element use COUNT()

SELECT COUNT(*) FROM tablename;

it's better to use aliases for the column names when using this functions.

pleasedontbelong
is it? i will be considering to use the aliases then. thank you for the tip.
Ibrahim Azhar Armar
A: 

try

select sum(amount), sum(cashpaid), sum(balance) from tablename
Yogesh
A: 

For counting the total number of records use count() function.

like:

select count(amount) from table_name;

It will return from above table in your question 3.

For Sum Use SUM() in SELECT query. Like:

select SUM(amount) as total_amount,SUM(cashpaid) as total_cash_paid,SUM(balance) as total_balance from table_name;

after as is your new column name which will automaticaly create after executing of query.

Ricky Dang