...

View Full Version : Math from Data



SlayerACC
01-03-2012, 06:59 PM
Hello how can you get the total of your products.

EG.. Database.

client_id, Product_id, Title, Cost

1,1, Product1, 25.00
1,2, Product2, 32.00
etc.

I was wondering how I can get them from the database and add them together to get a total cost.

Thanks in advance.

S.

BluePanther
01-03-2012, 07:03 PM
A quick google search returned this:
http://www.tizag.com/mysqlTutorial/mysqlsum.php

Exactly what you're looking for.

SlayerACC
01-03-2012, 07:20 PM
Hey BluePanther.

It is close... it worked partially..

but not there yet..

I need to sum that last column.. get that final total.

any thoughts.

S.

BluePanther
01-03-2012, 08:04 PM
Hey BluePanther.

It is close... it worked partially..

but not there yet..

I need to sum that last column.. get that final total.

any thoughts.

S.

I don't get what you mean. SUM(field) should be used to return the summation of the field through all rows. If you want to specify specific fields, use the WHERE clause. If you want to group it by different fields, use the group by.

SlayerACC
01-03-2012, 08:04 PM
I finally got it figured out..

Thanks for the help.


while ($row = mysql_fetch_assoc($result)){ $sum += $row['Value']; } echo $sum;

I was meaning get the sum of the column of data.

Thanks,

Slayer.

BluePanther
01-03-2012, 08:23 PM
I finally got it figured out..

Thanks for the help.


while ($row = mysql_fetch_assoc($result)){ $sum += $row['Value']; } echo $sum;

I was meaning get the sum of the column of data.

Thanks,

Slayer.

Not that you seem to care, lol, but the mysql SUM() function will do exactly that for you. Much faster, much more efficient.

SELECT SUM(column) as total FROM table

SlayerACC
01-03-2012, 09:16 PM
I do care...

I tried the example you gave and it did not work..

can you please explain.. I would love to know.

I could only get it to sum the product... which is fine..

but I also needed it to sum the column of products as well.

if there is a better way.. please fill me in.

Thanks, Slayer.

BluePanther
01-03-2012, 09:27 PM
Apologies, I just got the impression you didn't really care because you jumped the gun for another solution.

You want the total cost of all columns in the product table? So the total from the two-row example you gave in the first post would be 57?

SlayerACC
01-03-2012, 09:36 PM
I am looking for the cost of all the products the client_id has.

for instance.. if the client bought 2 products from me..

I want it to list..

the product number, the name, the cost.

then below the products purchased.. a total of the column of products purchased.. after that I will add the GST and sum the total.

then I have to figure out this decimal thing.. dang.

Yes I am always interested in the "way" to do things. I am no a pro.. just a guy who needs to get things done... not always to best way. but I need to learn.

Thanks again.. S.

BluePanther
01-03-2012, 09:43 PM
Misunderstood the requirements. Your way is probably alright, but you could add (SUM(cost) as totalCost) in the query that includes 'WHERE client_id=x'. That way, totalCost returned from the query will be the sum of cost for that client_id.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum