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.

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, 08: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, 08: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, 08: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, 08: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.