我用的MYsql 具体函数有一点点出入:而且你的表名建的太刺激了,都是in、out、return等关键字,我在表名上都添加了一个数字1,即 in1,out1,back1,return1,transfer1:
select
in1id,
in1amount -
if(in1id in(select `inId` from out1), (select sum(out1`amount`) from out1 where out1inId=in1id),0) +
if(in1id in(select out1`inId` from out1,back1 where out1id=back1outId), (select sum(back1`amount`) from back1,out1 where out1inId=in1id and out1id=back1outId),0) -
if(in1id in(select `inId` from transfer1),(select sum(transfer1`amount`) from transfer1 where in1id=transfer1inId),0)-
if(in1id in(select `inId` from return1), (select sum(return1`amount`) from return1 where return1inId=in1id),0) as total
from in1
经过测试后结果为:
id, total
1 490
2 486
3 489
希望能对你有所帮助
两张表如果是关联表,比如第一个表的sid对应第二个表的sid
用
select
from
表名1
a(a是表明的别名)
left
join
表名2
b
on
asid=bsid
;
如果没有关联
select
from
表1
,表2
你可以试试下面这个:
select from table as a inner (join) table_2 as b on (ach=bch) inner (join) table_3 as c on (cch=bch) inner (join) table_4 as d on (dch =cch) inner (join) table_5 as e on (ech = dch) inner (join) table_6 as f on (fch =ech) inner (join) table_7 as g on (gch=fch) inner (join) table_8 as h on (hch=gch) inner (join) table_9 as i on (ich =hch);
其实这个sql的查询命令你可以用一个循环来做,将字符串搞好就可以了。
以上就是关于mysql 多表运算的问题全部的内容,包括:mysql 多表运算的问题、mysql怎么多表查询两张不同的数据库表呢、MySQL多表查询(超过10个相关的表)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)