我有这些记录:
Table siswa
student_id | student_name |
---------------------------
1 | Abi |
2 | Bayu |
3 | Charles |
Table nilai
semester | subject | student_id | mark |
----------------------------------------
1 | 1 | 1 | 7 |
1 | 1 | 2 | 9 |
1 | 1 | 3 | 8 |
1 | 2 | 1 | 4 |
1 | 2 | 2 | 6 |
2 | 1 | 1 | 9 |
我将在第二学期和主题1中选择所有学生姓名。我预期的结果如下:
student_name | mark
-------------------
Abi | 9
Bayu | null
Charles | null
我已尝试过此查询,但它无法正常工作。我怎么能这样做?
SELECT s.name FROM siswa s LEFT OUTER JOIN nilai n ON s.student_id = n.student_id
WHERE n.semester = '2' AND n.subject = '1'
答案 0 :(得分:2)
也许你正在寻找这样的东西:
SELECT s.student_name, n.mark FROM siswa s
LEFT JOIN nilai n ON s.student_id = n.student_id AND semester = 2 AND subject = 1
您需要在left join
中处理结果。如果你在where
条款上执行此操作,那么您将删除它们。