简体   繁体   English

Android从两个表中的两个列中求和数量值,并使用求和值更新其中一个列中的数量值

[英]Android Sum Quantity values from two Columns in two tables and update the Quantity value in one of the Column with the summed value

I have two tables, Table A and Table B with Columns Qty_A and Qty_B 我有两个表,表A和表B的列为Qty_A和Qty_B

Table_A  
  Item_A1  Qty_A1      
  Item_A2  Qty_A2      


 Table_B
  Item_B1 Qty_B1      
  Item_B2 Qty_B2  

I want to run a query that will sum the Quantity values from both tables and update table A. So at the end of the Query, my Table_A will look like this 我想运行一个查询,该查询将对两个表中的“数量”值求和并更新表A。因此,在查询结束时,我的Table_A看起来像这样

Table_A  
  Item_A1      Qty_A1 + Qty_B1   
  Item_B1      Qty_A2 + Qty_B2


this is what my Query attempt looks like
insert into Table_A (Qty_A)
select Qty_B sum(QTY_A + QTY_B)
from Table_B
where Item_A = Item_B and Date_A = Date('now')

You want to update table_a and not insert new rows: 您要更新table_a而不插入新行:

update table_a 
set qty_a = qty_a + coalesce((
  select qty_b from table_b 
  where item_b = table_a.item_a), 0
)

See the demo . 参见演示 You have another condition in your code: 您的代码中还有另一个条件:

... and Date_A = Date('now')

which you don't mention earlier. 您之前没有提到。
Maybe you can add it as it is in the above query, or if you want something else you must clarify. 也许您可以按上述查询中的原样添加它,或者如果您需要其他内容,则必须进行澄清。
Note that the above code will work only if there is only 1 match between the columns item_a and item_b . 请注意,只有在item_aitem_b列之间只有1个匹配项时,以上代码才item_b If there would exist more matches then changes must be done to the code. 如果存在更多匹配项,则必须对代码进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM