【问题标题】:joining 3 or more table and sum the fields加入 3 个或更多表并对字段求和
【发布时间】:2018-03-30 02:48:24
【问题描述】:

我想加入 3 张桌子。

结果是其中一个字段 - 来自另一个表的 SUM 像这张图片,请帮助

【问题讨论】:

  • 你有没有尝试过?还是只是想让我们编写您的代码并完成您的作业?
  • 我已经使用了 colasce (),但是我需要在该 colasce 中添加更多条件,以便我可以得到类似附加的结果,如果您可以提供帮助?
  • 这里的大多数人想要格式化的文本,而不是图像。 (或者,更糟糕的是,图片链接。)
  • 问这个问题的方式不好。我建议您尝试谷歌搜索,尝试一些方法,然后在此处发布您尝试过的方法、有效的方法和无效的方法。
  • 添加您目前尝试过的查询

标签: mysql sql firebird


【解决方案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;

希望对你有帮助!

【讨论】:

  • 是的,它可以工作,但是不包含任何值的 t1 的 nmStudent 不会出现
  • 所以该名称的整行都没有出现?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2015-09-02
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多