加入3个或更多表并对字段求和

时间:2017-10-18 09:11:35

标签: mysql sql firebird

我想加入3个表。

结果是其中一个字段 - 来自其他表的SUM,如此图片,请帮助

Joining table and SUM field

1 个答案:

答案 0 :(得分:1)

您无需加入tblwork,因为您可以从其他2个表中获取所有必填字段。

以下查询应该有效:

select t1.nmstudent,
           sum(case when t2.idwork = 'w001' then t2.trprice else 0 end) as w001,
           sum(case when t2.idwork = 'w002' then t2.trprice else 0 end) as w002,
           sum(case when t2.idwork = 'w003' then t2.trprice else 0 end) as w003,
           sum(case when t2.idwork = 'w004' then t2.trprice else 0 end) as w004
    from tblstudent t1
    inner join tblTrans t2
    on t1.idstudent = t2.idstudent
    group by t1.idstudent;

希望它有所帮助!

相关问题