示例。我有产品表,其中有数量等。
item name: Shoes.
Quantity: 20
POS 表。如果用户购买了数量为20个的包。
Itenm name: Shoes
Quantity: 5
问题是。我如何将 POS 表中的数量:5 减去产品表中的数量 20。谢谢。
最佳答案
您可以根据项目名称连接两个表并进行减法。
select p.item_name,
p.quantity - po.quantity
from product p
join POS po on p.item_name = po.item_name;
如果您需要更新具有差异的产品表,则:
update product p
join POS po on p.item_name = po.item_name
set p.quantity = p.quantity - po.quantity;
如果有多行具有相同的项目名称,您可能希望先聚合它们,然后加入更新:
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;
关于java - 如何将 POS 表中的数量减去产品表中的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031681/