简体   繁体   English

IS NULL未在MYSQL列中找到所有空值

[英]IS NULL not finding all null values in column MYSQL

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 - COALESCE(c.credits, 0)
WHERE t.grade = 'F'
OR
t.grade IS NULL

I am trying to update tot_cred in student by subtracting the credit value of any class the student has failed, grade in takes relation = 'F', or is currently taking, grade in takes relation IS NULL. 我正在尝试通过减去学生未通过的任何班级的学分值来更新学生的tot_cred,成绩为“ F”或当前正在接受成绩为IS NULL。

The above query works for the single 'F' grade in the 'takes' relation. 上面的查询适用于“ takes”关系中的单个“ F”等级。 However it will only work for one of the 4 NULL values in the 'takes' relation in the grade column. 但是,它仅适用于成绩列中“ takes”关系中的4个NULL值之一。

Pretty new to SQL and I feel that something in the query is only looking for the first NULL value and not all of them. SQL很新,我觉得查询中的某些内容只是在寻找第一个NULL值,而不是全部。

Tables: 表格:

mysql> select ID, tot_cred
    -> from student;
+-------+----------+
| ID    | tot_cred |
+-------+----------+
| 00128 |      102 |
| 12345 |       32 |
| 19991 |       80 |
| 23121 |      110 |
| 44553 |       56 |
| 45678 |       38 |
| 54321 |       54 |
| 55739 |       38 |
| 70557 |        0 |
| 76543 |       58 |
| 76653 |       60 |
| 98765 |       98 |
| 98988 |      112 |
+-------+----------+

I want to change the tot_cred here if the corresponding grade is F or NULL 如果对应的成绩是F或NULL,我想在这里更改tot_cred

mysql> select ID, course_id, grade
    -> from takes
    -> order by grade;
+-------+-----------+-------+
| ID    | course_id | grade |
+-------+-----------+-------+
| 76543 | CS-001    | NULL  |
| 54321 | CS-001    | NULL  |
| 12345 | CS-001    | NULL  |
| 98988 | BIO-301   | NULL  |
| 98988 | BIO-101   | A     |
| 76543 | CS-319    | A     |
| 76543 | CS-101    | A     |
| 00128 | CS-101    | A     |
| 12345 | CS-190    | A     |
| 12345 | CS-315    | A     |
| 54321 | CS-101    | A-    |
| 55739 | MU-199    | A-    |
| 45678 | CS-319    | B     |
| 19991 | HIS-351   | B     |
| 98765 | CS-315    | B     |
| 45678 | CS-101    | B+    |
| 54321 | CS-190    | B+    |
| 44553 | PHY-101   | B-    |
| 12345 | CS-101    | C     |
| 76653 | EE-181    | C     |
| 23121 | FIN-201   | C+    |
| 98765 | CS-101    | C-    |
| 45678 | CS-101    | F     |
+-------+-----------+-------+

Here is where grade is. 这是成绩所在。 There are 4 NULL values and 1 F. 有4个NULL值和1F。

mysql> select course_id, credits
    -> from course;
+-----------+---------+
| course_id | credits |
+-----------+---------+
| BIO-101   |       4 |
| BIO-301   |       4 |
| CS-101    |       4 |
| CS-190    |       4 |
| CS-315    |       3 |
| CS-319    |       3 |
| CS-347    |       3 |
| EE-181    |       3 |
| FIN-201   |       3 |
| HIS-351   |       3 |
| MU-199    |       3 |
| PHY-101   |       4 |
+-----------+---------+

This is where I get the number of credits from. 这是我获得学分数量的地方。

Just realized that 3 of the NULL values are from a previous question in the lab. 刚刚意识到3个NULL值来自实验室中的上一个问题。 Those entries are not represented in the course relation. 这些条目未在课程关系中表示。 My problem is solved. 我的问题解决了。 I am very sorry for all of the wasted time. 对于所有浪费的时间,我感到非常抱歉。

Try to change it to this : 尝试将其更改为:

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

Check if you want to replace with INNER JOIN instead of LEFT JOIN as you might want to update only those records which as matching records. 检查是否要替换为INNER JOIN而不是LEFT JOIN,因为您可能只想更新那些匹配记录。

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

Check the fiddle here 这里检查小提琴

I did not realize that 3 of the NULL values were not represented in the course relation. 我没有意识到课程关系中没有3个NULL值。 This query works just fine, sorry for all the wasted time. 此查询工作正常,对于所有浪费的时间,我们深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM