A表 id,subid,paysubid
B表 id,subid,itemname,funds
C表 id,paysubid,payitemname,payfunds
A表数据: 1,3126,3319
B表数据: 1,3126,水费,7600
B表数据: 2,3126,电费,3000
C表数据:1,,3319 ,水费,200
C表数据:2,,3319 ,电费,300
C表数据:3,,3319 ,水费,500
C表数据:4,,3319 ,电费,300
C表数据:5,,3319 ,办公费,1000
想求出来的数据是
---------------------------
水费 7600 700
电费 3000 600
办公费 0 1000
语句应该怎么写呢。
------解决方案--------------------
select c.payitemname,b.funds,sum(c.payfunds)
from c left join b on c.payitemname=b.itemname
group by c.payitemname,b.funds
------解决方案--------------------
select * from (
(select itemname from b
union
select distinct payitemname from c)) a1
left join
(select itemname,sum(funds) from b group by itemname) a2 on a1.itemname=a2.itemname
left join
(select payitemname,sum(payfunds) from b group by payitemname) a3 on a1.itemname=a3.payitemname