选择,加入左,联盟。帮我,

时间:2013-06-21 10:29:38

标签: mysql

我有3个表流,我尝试构建一个查询来显示结果。但它恰好显示了一半。

Project
--------------
id    name
--------------
1     Project 1
2     Project 2
3     Project 3
4     Project 4
5     Project 5
6     Project 6


Pj_rp
-----------------------------
id    id_pj          id_rp
-----------------------------
1     1               1
2     2               2
3     1               3
4     2               4
5     1               5
6     3               6

Report
--------------
id    Fee
--------------
1     200
2     200
3     400
4     400
5     400
6     400

我想得到结果

**result**
--------------------
Project      SUM(Fee)
--------------------
Project 1     1000
Project 2      600
Project 3      400
Project 4     NULL
Project 5     NULL
Project 6     NULL

我构建了以下查询,但错误

SELECT 
    a.name, c.Fee
from
    Project a
        LEFT JOIN
    Pj_rp b ON (a.id = b.id_pj)
        LEFT JOIN
    Report c ON (b.id_rp = c.id)
GROUP BY a.tongmucdautuduan_usd

我不知道,谁能帮我解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

SELECT 
    a.name "Project", sum(c.Fee)
from
    Project a
        LEFT JOIN
    Pj_rp b ON (a.id = b.id_pj)
        LEFT JOIN
    Report c ON (b.id_rp = c.id)
GROUP BY a.name