加入 3 个或更多 table 并对字段求和
joining 3 or more table and sum the fields
我想加入 3 tables.
结果是其中一个字段 - 来自另一个字段的 SUM table 喜欢这张图片,请帮助
您无需加入 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;
希望对您有所帮助!
我想加入 3 tables.
结果是其中一个字段 - 来自另一个字段的 SUM table 喜欢这张图片,请帮助
您无需加入 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;
希望对您有所帮助!