需求:
查询 C 的结果,其中 C = A - B;
A的结果需要连表查询;
B的结果也需要连表查询;
实现:
过程一:
select sum(A) from 表a a
left join 表b b
on a.pid = b.id
where b.id = 1;
过程二:
select sum(B) from 表c c
left join 表d d
on c.pid = d.id
where d.id = 1;
结果:
select(m.A - n.B) as result
from
(select sum(A) as A from 表a a
left join 表b b
on a.pid = b.id
where a.id = 1) m,
(select sum(B) as B from 表c c
left join 表d d
on c.pid = d.id
where d.id = 1)n;