开发者

SQL statement MySQL

开发者 https://www.devze.com 2023-02-15 09:11 出处:网络
UPDATE product, claimdetail SET product.ProductQuantity = (product.ProductQuantity - claimdetail.quantity开发者_StackOverflow)
UPDATE product, 
       claimdetail 
   SET product.ProductQuantity = (product.ProductQuantity - claimdetail.quantity开发者_StackOverflow) 
 WHERE product.ProductId =  claimdetail.productcode

The code above is to deduct product from stock when staff is use product to claim it have a problem. If the staff use the same product 2 time it deduct only one time, how can I fix it?

Such as claimdetail have the product code "4712893150132" in many record it deduct only one record it should deduct all records.


UPDATE product
JOIN
(
SELECT productcode, SUM(quantity) SUMClaim
FROM claimdetail
GROUP BY productcode
) claims on claims.productcode = product.ProductId
SET product.ProductQuantity = product.ProductQuantity - claims.SUMClaim


UPDATE product SET product.ProductQuantity = (product.ProductQuantity - claimdetail.quantity) WHERE product.ProductId = claimdetail.productcode

0

精彩评论

暂无评论...
验证码 换一张
取 消