2013-03-04 140 views
3
UPDATE student as s 
LEFT JOIN takes as t 
    ON s.ID = t.ID 
LEFT JOIN course as c 
    ON t.course_id = c.course_id 
SET s.tot_cred = s.tot_cred - c.credits 
WHERE t.grade = 'F' OR t.grade IS NULL 

我试图通过减去学生失败的任何班级的学分值来更新学生的tot_cred,或者正在考虑在等级中的等级,一片空白。减法返回空值mysql

但是,对于符合此条件的任何学生,上面的查询将tot_cred设置为NULL,我无法弄清楚原因。

我很抱歉如果之前询问过,我试图寻找相关的东西,但找不到与减法有关的许多问题。我是新的stackoverflow。感谢大家的帮助。

回答

2

c.credits

set s.tot_cred = s.tot_cred - COALESCE(c.credits,0) 
+0

它为空值和F级的,还有3 null发生。我不相信这是导致这个问题的联盟。我会看看我的查询并试图弄清楚。感谢您对此问题的帮助! – 2013-03-04 06:36:45

1

添加COALESCE可以使用COALESCE像@JW答案,或使用IFNULL

UPDATE student as s 
LEFT JOIN takes as t 
    ON s.ID = t.ID 
LEFT JOIN course as c 
    ON t.course_id = c.course_id 
SET s.tot_cred = s.tot_cred - IFNULL(c.credits, 0) 
WHERE t.grade = 'F' OR t.grade IS NULL