在SQLite android数据库中减去两个表之间的数据



我在一个名为my_store.db的数据库中有两个数据表. 第一个表名是stock_table第二个表是sell_table.第一个表有4列。

股票表

<表类>代码名称价格总股票tbody><<tr>1的书5002002笔105003阅读表100020

子查询将在这里帮助您

update stock_table
set total_stock = total_stock - (select sum(sell_quantity) 
from sell_table st 
where product_code = st.product_code)

sum()函数在这里是为了防止每个产品在sell_table中可能有不止一行。你可以用sell_quantity代替它,如果你确定每个产品不会超过一个记录。

另外,如果您只需要更新一个产品的total_stock,您可以添加where子句:

update stock_table
set total_stock = total_stock - (select sum(sell_quantity) 
from sell_table st 
where product_code = st.product_code)
where product_code = 101

最新更新