[英]how to subtract quantity in my POS table to quantity in product table
example. 例。 i have product table which have a quantity etc.
我有具有数量等的产品表。
item name: Shoes.
Quantity: 20
POS table. POS表。 If the user bought the bag which have a quantity 20.
如果用户购买了数量为20的袋子。
Itenm name: Shoes
Quantity: 5
the question is. 问题是。 how can i subtract the quantity : 5 in POS table to the quantity 20 in product table.
我如何将POS表中的数量5减去产品表中的数量20。 thanks.
谢谢。
You could join the two tables on item name and do the subtraction. 您可以将两个表连接到项目名称上并进行减法。
select p.item_name,
p.quantity - po.quantity
from product p
join POS po on p.item_name = po.item_name;
If you need to update the product table with the difference, then: 如果您需要使用差异更新产品表,则:
update product p
join POS po on p.item_name = po.item_name
set p.quantity = p.quantity - po.quantity;
If there are multiple rows with same item name, you probably want to aggregation them first and then join to update: 如果有多个具有相同项目名称的行,则可能要先对其进行汇总,然后再加入以进行更新:
update product p
join (
select item_name,
sum(quantity) as quantity
from POS
group by item_name
) po on p.item_name = po.item_name
set p.quantity = p.quantity - po.quantity;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.