我有一个sRecords
包含一些数据的表和三个不同的表,每个表都有多行sRecordID
(PK)和一fees
列,
问题是
我怎样才能sum
从匹配的所有三个表中获得总费用sRecordID
?
fees
其他三个表是具有和sRecordID
(FK sRecords.sRecordID)的 cFees、oFees 和 iFees
编辑
我已经有了sRecordID
,所以我认为JOIN
这里不会有任何工作
select t.sRecordID,sum(t1.Fees)+sum(t2.Fees)+sum(t3.Fees) `Sum Fees`
from sRecords t
left join cFees t1 on t1.feeid=t.sRecordID
left join oFees t2 on t2.feeid=t.sRecordID
left join iFees t3 on t3.feeid=t.sRecordID
group by t.sRecordID
SELECT
sRecords.sRecordID,
SUM(cFees.fees) + SUM(oFees.fees) + SUM(iFees.fees) as total_fees
FROM sRecords
LEFT JOIN cFees ON sRecords.sRecordID = cfees.sRecordID
LEFT JOIN iFees ON sRecords.sRecordID = ifees.sRecordID
LEFT JOIN oFees ON sRecords.sRecordID = ofees.sRecordID
GROUP BY sRecords.sRecordID