Jump to content

PHP/MySQL sum() works but need sum with multiple fields


mo

Recommended Posts

Hopefully I get at least one response this time.

 

I have a query that returns the total amount of items in a shopping cart:

 

$sqlStr = mysql_query("select SUM(`price`) as total from `mr_cart`

                  WHERE `cart_session_id` = '$sid'") or die(mysql_error());

 

The table mr_cart has two fields, price and quantity. I need to get the total for each line's price * quantity.

 

Anyone???

Link to comment
Share on other sites

 cart_id int(11)            

 smid int(11)          

 store_name varchar(40)          

 cart_session_id char(32)              

 cart_qty decimal(10,0)            

 price float              

 cart_date datetime            

 title varchar(40)                

 options longtext            

 comments longtext

 

I was trying to avoid doing the calculation in post processing after the query, for performance reasons. I prefer to do the sum (price * cart_qty) in the query, if possible.

 

Thanks...

Link to comment
Share on other sites

Thanks Frost. That did the trick, it works perfectly now.

 

I actually thought of doing it the way you suggest but didn't even try as I thought the syntax was incorrect. I guess I was wrong. I'll experiment next time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.