一对多三表关联汇总SQL问题

2024-11-06 12:29:21
推荐回答(2个)
回答1:

可答明以把清桥告b表和消族c表先去重
select
a.id,
a.name,
sum(b.value) value,
sum(c.value) value2
from A a
left join (select id,value from B group by id,value) b on a.id=b.aid
left join (select id,value from C group by id,value) c on a.id=c.aid
group by a.id,a.name

回答2:

select 
        a.id,a.name,
        sum(b.value) value,
        (select sum(c.value) from C c where 尺姿a.id=c.aid)  value2 
from A a 
left 陵老绝join B b on a.id=b.aid
group by a.id,a.name
order by 含郑a.id,a.name