简体   繁体   English

如何将我的POS表中的数量减去产品表中的数量

[英]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.

相关问题 从购物车更新产品表中的库存数量 - Updating stocks quantity in Product Table from Cart 当我通过 POST 方法中的 @RequestBody 传递我想要的某个产品的数量时,如何实现一种方法来减去和更新库存? - How can I implement a method to subtract and update stock when I pass the quantity I want of a certain product through @RequestBody in POST method? (更新)JAVA:在表列中添加数据的数量 - (UPDATE) JAVA: Adding the quantity of a data in table column Android Studio Java:如果产品已经存在于我的购物车活动中,我该如何更新我的数量? - Android Studio Java: How can I update my quantity if the product already exist's in my Cart Activity? 为什么在 java 中第二个产品数量覆盖第一个产品数量? - Why the second product quantity override the first product quantity in java? 用 java 更新数据库中的产品数量 - update quantity of product in database with java 如果产品已经进入数据库,如何只增加数据库中的数量? - How to only increase quantity in database if the product is already entered to the database? 如何更新房间数量? - How to Update Room Quantity? 如何根据Java Netbeans中的项目类型减去两个表之间的数量? - How to subtract quantity between two tables based on item types in Java Netbeans? 当表1数量字段大于0时,将表1中的数据插入到表2中 - Insert data from table1 to table 2 when table 1 quantity field greater than 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM