
select
in1.id,
in1.amount -
if(in1.id in(select `inId` from out1), (select sum(out1.`amount`) from out1 where out1.inId=in1.id),0) +
if(in1.id in(select out1.`inId` from out1,back1 where out1.id=back1.outId),(select sum(back1.`amount`) from back1,out1 where out1.inId=in1.id and out1.id=back1.outId),0) -
if(in1.id in(select `inId` from transfer1),(select sum(transfer1.`amount`) from transfer1 where in1.id=transfer1.inId),0)-
if(in1.id in(select `inId` from return1), (select sum(return1.`amount`) from return1 where return1.inId=in1.id),0) as total
from in1
经过测试后结果为:
id, total
1.490
2.486
3.489
希望能对你有所帮助
UPDATEA
SET
A.数量 = A.数量 - B.退货数量
FROM
A JOIN B
ON (A.物料编号 = B.物料编号)
上面这种2表关联更新写法
对于 A 表 与 B 表, 是 1对1 的情况下, 是没有问题的。
对于 A 表与 B 表, 是 1对多的情况下, 就没有测试过了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)