MySQL: Update column with sum of other columns

What is wrong with this MySQL query?
UPDATE `sales` SET `sale_received` =  sum(`sale_products_subtotal` + `sale_shipping_charged` + sale_tax) WHERE `sale_payment_status` = 'Paid in Full'

Open in new window

It returns an error:
Error: Invalid use of group function
LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
It's hard to say exactly without knowing the schema of your table.  I suspect you only want to sum sale by sale that are paid in full and not a sum of all sales that are paid in full.  In order to keep the sum to the sale itself and not an overall sum, you'll need to group by the sale id.

UPDATE sales AS s
 (SELECT sale_id,
   SUM(sale_products_subtotal + sale_shipping_charged + sale_tax) as sum_sale_received
    WHERE sale_payment_status = 'Paid in Full' 
    GROUP BY sale_id) AS grp
   ON grp.sale_id = s.sale_id
SET s.sale_received = grp.sum_sale_received

Open in new window

Maybe you should try this:

UPDATE `sales` SET `sale_received` =  
   CONVERT(`sale_products_subtotal`,DECIMAL(18,9)) +
   CONVERT(`sale_shipping_charged`,DECIMAL(18,9))  + 
   CONVERT(`sale_tax`, DECIMAL(18,9)) 
WHERE `sale_payment_status` = 'Paid in Full';

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
Thank you both. zephyr_hex, your idea returned an error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE sale_payment_status = 'Paid in Full' GROUP BY sale_id) AS grp ON' at line 5

themrrobert, your idea worked.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.