【问题标题】:how to subtract quantity in my POS table to quantity in product table如何将我的 POS 表中的数量减去产品表中的数量
【发布时间】:2017-03-26 16:53:20
【问题描述】:

例子。我有产品表,其中包含数量等。

item name: Shoes.
Quantity: 20

POS 表。如果用户购买了数量为 20 个的包。

Itenm name: Shoes
Quantity: 5

问题是。如何将数量:POS 表中的 5 减去产品表中的数量 20。谢谢。

【问题讨论】:

  • 请显示您已经尝试过的任何代码。

标签: java mysql netbeans


【解决方案1】:

您可以将两个表加入项目名称并进行减法。

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;

【讨论】:

  • 我该把代码放在哪里?在一个按钮或?对不起,只是一个初学者。
  • @Jay - 嗯.. This 将帮助您入门。
猜你喜欢
  • 2018-06-02
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 2014-07-19
  • 2019-11-06
相关资源
最近更新 更多