I have five(5) tables: students, subjects, period_one, period_two and period_three. I'm performing a left join to select values from these five tables:
Students Table (students)
student_id | Name
--------------|-------
1 |John
2 |Peter
3 |Flomo
Subjects Table (subjects)
subject_id |SubjectName
-----------------|-------
math101 |Mathematics
eng201 |English
lang303 |Language Arts
Period One Table (period_one)
id|student_id |subject_id| score
--------------|----------|-----
1 |1 | math101 |99
2 |2 | eng201 |88
3 |3 | lang303 |77
Period Two Table (period_two)
id|student_id |subject_id| score
--------------|----------|-----
1 |1 | math101 |100
2 |2 | eng201 |60
3 |3 | lang303 |65
Period Three Table (period_three)
id|student_id |subject_id| score
--------------|----------|-----
1 |1 | math101 |71
2 |2 | eng201 |51
3 |3 | lang303 |71
Here is the query I'm using to retrieve records Query1:
SELECT period_one.student_id, period_one.subject_id, period_one.score, period_two.score,period_three.score
from period_one
LEFT JOIN period_two
ON period_one.subject_id = period_two.subject_id
AND period_one.student_id = period_two.student_id
LEFT JOIN period_three
on period_one.subject_id = period_three.subject_id
AND period_one.student_id = period_three.student_id
WHERE period_one.student_id = 10
The problem with the code above is if the student id I'm looking for is not in the very first table(periodOne) the left join is being apply to the query returns null even if records of that student are in the others tables(periodTwo and periodThree). I look the issue up(https://www.w3schools.com/sql/sql_join_left.asp) and verified that is was not the best way to do things.
I THEN MADE SOME CHANGES
So, I updated my query to look like this (Query2):
SELECT students.student_id, period_one.score, period_one.subject_id,
period_two.score, period_two.subject_id, period_three.score,
period_three.subject_id
from students
LEFT JOIN period_one
ON students.student_id = period_one.student_id
LEFT JOIN period_two
ON students.student_id = period_two.student_id
LEFT JOIN period_three
ON students.student_id = period_three.student_id
WHERE students.student_id = 3 OR period_one.student_id = 3 OR period_two.student_id = 3 OR period_three.student_id = 3
This works perfectly and since the students table is the main table which all the period tables are referencing. With this if a student id is not within the period_one and period_two table but period_there, the studentId, subjectId, and score is return for that table.
THEN ANOTHER PROBLEM POPS OUT
Before I updated my code I was displaying records a bit the way I wanted it, but wasn't getting/retrieving records the desire way. That was what prompted me to change my query, because I noticed it was the issue.
Now, based on my first query(Query1) I was selecting the subject_id from the various tables in the select statement. When I'm displaying records I passed that subject_id return from the query into a function that gets the subject name for that id. This is how I was displaying my results:
This works if the student id is within the table assign to the from clause, otherwise it returns nothing. That was why I change my code.
But now that I have change my code to (Query2) I'm unable to display the subject id and its name because it is not within the students table. Here is a gist on how I'm displaying my records: https://gist.github.com/nathansiafa/e9d22791800d4ba3a00e2b98de52baec
Is there a way which I can make it work better? Will appreciate suggestions as well as feed backs. Thanks!