-1

我有一个sRecords包含一些数据的表和三个不同的表,每个表都有多行sRecordID(PK)和一fees列,

问题是
我怎样才能sum从匹配的所有三个表中获得总费用sRecordID

fees其他三个表是具有和sRecordID(FK sRecords.sRecordID)的 cFees、oFees 和 iFees

编辑
我已经有了sRecordID,所以我认为JOIN这里不会有任何工作

4

2 回答 2

1
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
于 2013-07-29T04:55:44.807 回答
0
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
于 2013-07-29T04:55:37.223 回答