2017-07-21 53 views
0

我有四個表在MySQL一樣用SUM()

T1: division(divisionID, division_name) 
T2: work(workID, divisionID, category, work_desc, value) 
T3: approved_work(approve_id, workID, approved_on) 
T4: payment(payment_id, workID, amount_paid, paid_date) 

現在加入多個表的,我希望有一個報告,即。

T1.division_name, T2.category, T2.work_desc, T2.value, T3.approved_on, sum(amount_paid) 

請在SQL查詢

+1

加入表(也許左連接)。做一個小組。祝你好運! – jarlh

回答

0

幫助試試這個:

SELECT 
T1.division_name, 
T2.category, 
T2.work_desc, 
T2.value, 
T3.approved_on, 
sum(T4.amount_paid) 
FROM T1 
LEFT JOIN T2 ON T1.division_id=T2.division_id 
LEFT JOIN T3 ON T2.work_id=T3.work_id 
LEFT JOIN T4 ON T2.work_id=T4.work_id 
GROUP BY T1.division_name, 
T2.category, 
T2.work_desc, 
T2.value, 
T3.approved_on;