简体   繁体   English

MySQL连接,左连接

[英]MySQL joins, left join

So am trying to write a MySQL query with a simple join to pull things from two different tables. 因此,我试图编写一个具有简单联接的MySQL查询,以从两个不同的表中提取内容。

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. 我想拉所有的学生以及得分的分数。item_id= 1,如果他们有一个得分,如果他们不得分,我仍然希望它为学生拉信息,好吧,至少在那里命名。

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的学生,结果看起来像

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

?

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. 您会看到所有其他所有行的scores.item_idNULL ,从而使您的WHERE scores.item_id = 1子句失败,因此这些行不会出现在您的结果中。

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)

您需要将联接的列添加到您的选择-列

The where clause you have put on your SQL query is limiting the result set to one row. 您在SQL查询中放置的where子句将结果集限制为一行。 Try removing the where clause. 尝试删除where子句。

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

您提供了一个条件过滤条件,以除去记录,从而删除了您应该获得所需结果的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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