How to get the sum of the column?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

How to get the sum of the column?

Post by MicroBoy »

How to get the sum of the column, for example like in the photo?

Image
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: How to get the sum of the column?

Post by manohoo »

When I learned Oracle's SQL there was a REPORT feature that allowed you to do what you want. However, in MySQL the only way that I know how is to run 2 separate queries. In your case it would be something like this:

Code: Select all

$query1 = "SELECT Muaji, Fitimi, Bortxh, Fitimi_Total from TABLE";
$query2 = "SELECT Sum(Fitimi_Total) from TABLE ";
Then do your usual HTML/PHP to bring'em together into a table.
bytebrite
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 1:42 pm

Re: How to get the sum of the column?

Post by bytebrite »

Assuming mysql and that you want the sum of last column only, you can do it in one query:

Code: Select all

select label, b, c, d, sum(b + c + d) from scratch group by label with rollup
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: How to get the sum of the column?

Post by MicroBoy »

manohoo wrote:When I learned Oracle's SQL there was a REPORT feature that allowed you to do what you want. However, in MySQL the only way that I know how is to run 2 separate queries. In your case it would be something like this:

Code: Select all

$query1 = "SELECT Muaji, Fitimi, Bortxh, Fitimi_Total from TABLE";
$query2 = "SELECT Sum(Fitimi_Total) from TABLE ";
Then do your usual HTML/PHP to bring'em together into a table.
I think that there is a problem to do this, Because to show this data in the table, I use the above code. To be more clear, I have all the sales in a table in MySQL, then Here I collect all the sales from "November - 2009", "December 2009" etc... So Now I want to calculate all the sales of 2009, all the sales that are in the Table(in photo).

The Code:

Code: Select all

$sql = mysql_query("SELECT SUM(qtotal), SUM(borxh), DATE_FORMAT(data, '%m/%Y') AS dataere FROM riparimet GROUP BY dataere ORDER BY data DESC");
p.s. In the photo, the row where it's "Nëntor - 2008" it's wrong, cause really it's "Gusht - 2009"
bytebrite
Forum Newbie
Posts: 5
Joined: Tue Dec 29, 2009 1:42 pm

Re: How to get the sum of the column?

Post by bytebrite »

Manny7
Forum Newbie
Posts: 3
Joined: Wed Dec 23, 2009 9:45 am

Re: How to get the sum of the column?

Post by Manny7 »

just

Code: Select all

 
$sql = mysql_query("SELECT Sum(Fitimi_Total) from TABLE");
 
don't go ???
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: How to get the sum of the column?

Post by MicroBoy »

Thanks a lot for help, I made another query too, and did not group data in the other query. Because in the first query I grouped them.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: How to get the sum of the column?

Post by manojsemwal1 »

do one thing take the $Fitimi_Total value in loop and add like

before loop define $i=0;
inside the Loop $Fitimi_Tetal=$Fitimi_Tetal+$i;

and the Last just print Total $Fitimi_Tetal
Post Reply