我有两张桌子股票和促销
库存
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", "pradyut");
jsonObject.put("password", "5F4DCC3B5AA765D61D8327DEB882CF99");
} catch (JSONException e) {
e.printStackTrace();
Log.i("log", "json exception : " + e.toString());
}
if (jsonObject == null) {
Log.i("log", "the json object is not null");
// this part is showing everytime.
} else {
Log.i("log", "the json object is null");
}
出售
id Name size Qty
1 box1 10 100
2 box2 12 200
3 box3 14 500
4 box4 16 700
我想在Subtract Qty
之后得到这个结果库存
id Name size Qty
1 box1 10 1
2 box2 12 2
3 box3 14 5
4 box4 16 7
帮助!
答案 0 :(得分:0)
如果你想存储结果在mysql中你可以(使用关系名称)更新
update Stock as a
inner join Sale as b on a.name = b.name
set a.Qty = a.Qty -b.Qty
或使用id作为关系
update Stock as a
inner join Sale as b on a.id = b.id
set a.Qty = a.Qty -b.Qty
或者如果你只想要一个选择,你可以
select a.id, a.name, a.size, a.Qty -b.Qty as qty
from fromStock as a
inner join Sale as b on a.id = b.id
在sqlite中,不允许使用内部联接进行更新,因此您可以使用子选择
update Stock
set Qty = ( select Stock.Qty -Sale.Qty from Stock
Inner join Sale on Sale.name = Stock.name )
WHERE
EXISTS (
SELECT *
from Stock
Inner join Sale on Sale.name = Stock.name
)
答案 1 :(得分:0)
您可以按id
和name
加入表格,然后简单地减去Qty
值。添加了table alias名称以提高可读性
SELECT ST.id, ST.Name, ST.size, (ST.Qty - SA.Qty) AS Qty
FROM Stock ST
INNER JOIN Sale SA ON SA.id = ST.id AND SA.Name = ST.Name
如果您不需要按Name
加入,则只能id
加入
SELECT ST.id, ST.Name, ST.size, (ST.Qty - SA.Qty) AS Qty
FROM Stock ST
INNER JOIN Sale SA ON SA.id = ST.id
SQLite的更新
UPDATE Stock
SET Qty = ( SELECT ST.Qty - SA.Qty
FROM Stock ST
INNER JOIN Sale SA ON SA.id = ST.id )
WHERE id IN (SELECT id FROM Stock )
的参考资料
答案 2 :(得分:0)
试试这个:
Table A Table B
ID, ABC ABC
1, A A
2, B A
3, C C
4, D E
5, E F
答案 3 :(得分:0)
使用以下查询
Select table.id,table.name,table.size,sum(table.qty) from
(Select id,name,size,qty from stock
Union
Select id,name,size,qty*-1 from sale) as table group by table.id,table.name,table.size