Sql Access:如何在一个查询中汇总多个表的字段?

Sql Access:如何在一个查询中汇总多个表的字段?,sql,ms-access,ms-access-2016,Sql,Ms Access,Ms Access 2016,我有一个母表(tblMother),它由以下字段组成: id,文本 我有多个子表: tblChildOne:id,motherID,value tblChildTwo:id,motherID,value tblchildtree:id,motherID,value 看起来所有子表都具有相同的数据结构。他们没有。我只想把复杂的表格分解到关键点。考虑所有表的长度不同,它们有许多其他字段,但ID、母ID和值是相同的。 我要找的是一个查询,它为特定的motherID汇总每个子表的值。比如说: mothe

我有一个母表(tblMother),它由以下字段组成:

id,文本

我有多个子表:

tblChildOne:id,motherID,value

tblChildTwo:id,motherID,value

tblchildtree:id,motherID,value

看起来所有子表都具有相同的数据结构。他们没有。我只想把复杂的表格分解到关键点。考虑所有表的长度不同,它们有许多其他字段,但ID、母ID和值是相同的。

我要找的是一个查询,它为特定的motherID汇总每个子表的值。比如说:

motherID文本SumValueChild1 SumValueChild2 SumValueChild3

1“测试1”200 300 400

2“测试2”150 450 300

3“测试3”112 235 472


我很确定我必须连接多个表,但是我没有得到正确的结果。感谢您的帮助。

您可以使用相关子查询:

select m.*
       (select sum(child1.value) from child1 where child1.motherID = m.motherID) as sum1,
       (select sum(child2.value) from child2 where child2.motherID = m.motherID) as sum2,
       (select sum(child3.value) from child3 where child3.motherID = m.motherID) as sum4
from mother m;