Access:如何在一个查询中汇总多个表的字段?
Access: How to sum up fields of multiple tables in a query?
我有一个母亲 table (tblMother),它由以下字段组成:
id, text
而且我有多个 child tables:
tblChildOne: id, motherID, value
tblChildTwo: id, motherID, value
tblChildThree: id, motherID, value
看起来所有 child table 都具有相同的数据结构。他们还没有。我只想将复杂的 table 分解为要点。考虑到所有 table 都有不同的长度,并且除了 id、motherID 和 value 之外,它们还有许多其他字段。
我正在寻找的是一个查询,它汇总了特定 motherID 的每个 child table 的值。所以像这样:
motherID 文本 SumValueChild1 SumValueChild2 SumValueChild3
1 "测试 1" 200 300 400
2“测试2”150 450 300
3 "测试3" 112 235 472
我很确定我必须加入多个 table,但我没有得到正确的结果。感谢您的帮助。
您可以使用相关子查询:
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;
我有一个母亲 table (tblMother),它由以下字段组成:
id, text
而且我有多个 child tables:
tblChildOne: id, motherID, value
tblChildTwo: id, motherID, value
tblChildThree: id, motherID, value
看起来所有 child table 都具有相同的数据结构。他们还没有。我只想将复杂的 table 分解为要点。考虑到所有 table 都有不同的长度,并且除了 id、motherID 和 value 之外,它们还有许多其他字段。
我正在寻找的是一个查询,它汇总了特定 motherID 的每个 child table 的值。所以像这样:
motherID 文本 SumValueChild1 SumValueChild2 SumValueChild3
1 "测试 1" 200 300 400
2“测试2”150 450 300
3 "测试3" 112 235 472
我很确定我必须加入多个 table,但我没有得到正确的结果。感谢您的帮助。
您可以使用相关子查询:
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;