I have my tables like that:
[users] [chapters] [exam_scores]
- id - title - fk_chapter
- fk_user
- grade
I need a query to return all the chapter titles and where available the grade of the user. Here is my try:
select chapters.title as Title, exam_scores.grade as Results
from users, exam_scores left join chapters
on chapters.id = exam_scores.fk_chapter
where exam_scores.fk_user = users.id and users.id = 15;
That one returns records if there is available grade, but not the rest of the chapters.
Since you want to get a list of all the chapters, regardless of whether or not there is a user score for the chapter. Because of that, you want to SELECT from the chapters table. Then, since you want a result for each chapter, you need to LEFT JOIN the exam_scores table so you will get records for rows that have scores, and records for scores that don’t. When using a natural join (
JOINorFROM table, table2, table3) records are only returned for which there are matching left and right values.So, your query should look like this:
Notice, you don’t need to even include the
userstable here, as you’re not accessing any information for it. Your query was joining the users table and then doing theWHEREclause on it’sidkey. This is not required, unless you’re getting data out of the users table. If you do, in fact, need data out of the user’s table, you’d LEFT JOIN it onto the exam_scores table like so:Hope that helps and sheds some light as to how you should structure your queries and JOINs better in the future.
edit: I edited my answer in response to your comment. It makes complete sense why you wouldn’t be getting responses without a grade attached, and here’s why:
When you join the 2 tables together, they’re being joined by the user id. That’s fine, because when the user id doesn’t map over to the chapters table, you get a
NULLvalue for the user id and grade. However, since in ourWHEREclause we are filtering for the user with id15, it filters out all theNULLvalues for the user columns. So, to fix against this, you add anORstatement to yourWHEREclause that says a user id ofNULLis okay.