PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How do i update sql value with sum comparison

 October 24, 2022     sql-update     No comments   

Issue

I am trying to update a value to show the picked status of an order based on the picked status against the order qty. The data is in the same table but i cannot figure out the correct syntax. I tried: Update Orders set Status = 'FULL' where Sum(Qty_Order) = sum(Qty_Picked) How can i apply this logic using an aggregate query? Thanks in advance for any help/


Solution

One approach uses an update join:

UPDATE Orders o1
INNER JOIN
(
    SELECT id
    FROM Orders
    GROUP BY id
    HAVING SUM(Qty_Order) = SUM(Qty_Picked)
) o2
    ON o2.id = o1.id
SET
    Status = 'FULL';

This assumes that your Orders table has a column id which uniquely identifies each order.



Answered By - Tim Biegeleisen
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing