如何对父ID值求和并显示为null

时间:2018-07-31 08:28:57

标签: mysql

嗨,我有查询返回我的数据

SELECT
     s.StandardID,
     s.StandardName as Standard,
     TableA.Score,
     Dropdown1.Name as dq,
     Standard.ParentStandardID,         
     Standard.Weight     
FROM TableA
      JOIN Standard ON Standard.StandardID = TableA.StandardID

结果

+------------+----------+-------+------------------+---------+
| StandardID | Standard | Score | parentstandardid |  weight |
+------------+----------+-------+------------------+---------+
|    1       |     A    |       |      3           |    o    |
+------------+----------+-------+------------------+---------+
|   107      |     B    |       |     NULL         |    1    |
+------------+----------+-------+------------------+---------+
|   111      |     C    |       |      1           |   107   |
+------------+----------+-------+------------------+---------+
|   112      |     D    |       |      2           |   107   |
+------------+----------+-------+------------------+---------+

现在分数为零的地方,我想显示核心父母ID的分数总和。如果standardID为107,且得分比null为零,则应为父母ID的总和107。如果不为null或得分,则不使用分组依据。

任何帮助将不胜感激 谢谢!

1 个答案:

答案 0 :(得分:0)

选择

S.StandardID,   (当A.Score为NULL则C.Score ELSE A.Score结束的情况)AS New_Score

FROM表A

内部联接标准S ON S.StandardID = TableA.StandardID 左外连接 (  SELECT S1.ParentStandardID,SUM(B.ScoreValue)得分  在表B中,标准S1,其中S1.StandardID = B.StandardID  GROUP BY ParentStandardID ) C 开启S.StandardID = C.ParentStandardID