来自连接查询的sql update表

时间:2014-07-04 20:48:08

标签: sql

嗨我的表格如下所示,我需要从另一个表格中的数据更新空均值标记,以便学生完成的所有作业的平均值将填充该字段。我可以使用Isql作为dbms来建议如何做到这一点吗?

enrolment-    
student_id- course code - averagemark 
    1              a1           -   0
    2              b2           -   0
    3              c3           -   0
    4              d4           -   0

assignment-
    student_id- course code -      assignment number    mark 
        1              a1                  1            -   50
        1              b2                  2            -   55
        2              a1                  1            -   60
        2              b2                  2            -   65

1 个答案:

答案 0 :(得分:0)

此示例基于Microsoft SQL Server。从How can I do an UPDATE statement with JOIN in SQL?复制样本:

update u
set u.assid = s.assid
from ud u
    inner join sale s on
        u.id = s.udid

尝试这样的事情(在Student表中提供列的名称是AverageMark)

UPDATE s
SET s.AverageMark = AVG(mark)
FROM Student s LEFT OUTER JOIN enrolment e
    on e.Student_Id = e.Student_Id
               LEFT OUTER JOIN assignment a
    ON e.student_id = a.student_id
AND e.course_code = a.course_code
GROUP BY e.student_id, e.course_code