mysql 多表运算的问题

mysql 多表运算的问题,第1张

我用的MYsql 具体函数有一点点出入:而且你的表名建的太刺激了,都是in、out、return等关键字,我在表名上都添加了一个数字1,即 in1,out1,back1,return1,transfer1:

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

希望能对你有所帮助

UPDATE

A

SET

A.数量 = A.数量 - B.退货数量

FROM

A JOIN B

ON (A.物料编号 = B.物料编号)

上面这种2表关联更新写法

对于 A 表 与 B 表, 是 1对1 的情况下, 是没有问题的。

对于 A 表与 B 表, 是 1对多的情况下, 就没有测试过了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8628846.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-19
下一篇2023-04-19

发表评论

登录后才能评论

评论列表(0条)

    保存