使用SUM()连接多个表

时间:2017-07-21 07:25:28

标签: mysql sql join

我在MySQL中有四个表,如

AM-#1234567-v1-filename.MSG

现在,我想要一份报告,即

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)

请在SQL查询中提供帮助

1 个答案:

答案 0 :(得分: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;