开发者

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.

How do i count and sum the values from the database

I want to sum up the values of a particular column, for example i want to calculate 开发者_如何学JAVAthe total amount, total cashpaid and total balance. hwo do i do it?


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;


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.


Try out this:

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


try

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜