Beamer180
Beamer180

Reputation: 1525

MySQL joins, left join

So am trying to write a MySQL query with a simple join to pull things from two different tables.

The fist table is a students table (Sorry its not prettier I couldn't find a better way to put them in)

id  first_name  last_name   major    phone_number
-------------------------------------------------
1   Eric        Larsen      CS       1234567891
2   Sam         Coons       English  1234567891
3   David       Brown       MAE      1234567891
4   Richard     Brown       CS       1234567891
5   Kendra      Griffiths   FCHD     1234567891

The second is a scores table, rich now it just has one row,

id  student_id  item_id  score
------------------------------
1   1           1        20

There is a third table with a list of items but is not really needed for this particular query.

I want to pull all of the students along with there score for scores.item_id = 1 if they have a score, if they don't I still want it to pull the information for the student, well, there name at least.

So here is the query I wrote.

SELECT students.first_name, students.last_name, scores.score
FROM students
     LEFT JOIN scores
     ON students.id = scores.student_id
WHERE scores.item_id =1

In my mind this should work, it should pull all the students even if there is no score for them because it is a left join but it is only pulling the one student with a score Eric Larsen, The results looks like

Eric, Larsen, 20

But shouldn't it be:

first_name  last_name  score
----------------------------
Eric        Larsen     20
Sam         Coons      NULL
David       Brown      NULL
Richard     Brown      NULL
Kendra      Griffiths  NULL

?

Upvotes: 0

Views: 174

Answers (4)

trapper
trapper

Reputation: 12003

This query will reveal your problem.

SELECT students.first_name, students.last_name, scores.score, scores.item_id
    FROM students
    LEFT JOIN scores
        ON students.id = scores.student_id

You see scores.item_id is NULL for all those other rows, so that fails your WHERE scores.item_id = 1 clause, hence those rows not appearing in your results.

This should get the results you are after

SELECT students.first_name, students.last_name, scores.score
    FROM students
    LEFT JOIN scores
        ON (students.id = scores.student_id)
    WHERE (scores.item_id = 1 OR scores.item_id IS NULL)

or

SELECT students.first_name, students.last_name, scores.score
    FROM students
    LEFT JOIN scores
        ON (students.id = scores.student_id AND scores.item_id = 1)

Upvotes: 1

Ajith Sasidharan
Ajith Sasidharan

Reputation: 1155

You have provided a where condition to filter out the records remove that where condition you should get you desired results then.

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

The where clause you have put on your SQL query is limiting the result set to one row. Try removing the where clause.

SELECT students.first_name, students.last_name, scores.score
    FROM students
    LEFT JOIN scores
        ON students.id = scores.student_id;

Upvotes: 0

duffy356
duffy356

Reputation: 3716

you need to add the joined column to your select - columns

Upvotes: 0

Related Questions