Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

How do i count and sum the values from the database

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?

Upvotes: 0

Views: 6125

Answers (5)

Gitesh Dang
Gitesh Dang

Reputation: 182

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.

Upvotes: 1

Nik
Nik

Reputation: 4075

try

select sum(amount), sum(cashpaid), sum(balance) from tablename

Upvotes: 1

pleasedontbelong
pleasedontbelong

Reputation: 20102

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.

Upvotes: 2

Murugesh
Murugesh

Reputation: 820

Try out this:

select SUM(amount) AS total_amount, SUM(cashpaid) AS total_cashpaid, SUM(balance) AS total_balance  from tablename;

Upvotes: 1

shamittomar
shamittomar

Reputation: 46692

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;

Upvotes: 4

Related Questions