简体   繁体   中英

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.

The above query works for the single 'F' grade in the 'takes' relation. However it will only work for one of the 4 NULL values in the 'takes' relation in the grade column.

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.

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

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.

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. 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.

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. This query works just fine, sorry for all the wasted time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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