简体   繁体   English

从不同的表中减去两列并更新库存

[英]subtract two columns from different tables and update the inventory

I need to update the product quantity in store inventory table based on quantity sold in SALELINE table 我需要根据SALELINE表中销售的数量更新商店库存表中的产品数量

so, I want to subtract QTYONHAND from Store_inv and QTYSOLD from SALELINE table for each product which has a Product_id 所以,我想从SALELINE表中为Store_inv和QTYSOLD减去每个具有Product_id的产品的QTYONHAND

Store_inv and SALELINE both has a Product_id as (FK) Store_inv和SALELINE的Product_id都为(FK)

I didn't clear what data you have in table.Based on your question assumption 我没有清楚你在表格中有什么数据。基于你的问题假设
I posted below query.First Subtract qty from two tables using subquery than update inventory table 我在下面发布了query.First使用子查询从两个表中减去数量而不是更新库存表

UPDATE store_inventory SET product_quantity = A.SubQty
FROM 
(
   SELECT ( QTYONHAND - QTYSOLD ) SubQty,SALELINE.Product_id AS Product_id
   FROM Store_inv
   JOIN SALELINE ON Store_inv.Product_id = SALELINE.Product_id
) A 
WHERE A.Product_id = store_inventory.Product_id

Try this: 尝试这个:

UPDATE SALELINE s
INNER JOIN store_inv si ON s.product_id = si.product_id
SET s.product_quantity = (si.QTYONHAND-s.QTYSOLD)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM