Sql连接查询返回未指定字段值的记录

时间:2017-03-13 15:23:25

标签: php mysql sql join

大家好,我有三张桌子,我正在做一个JOIN来从这些表中返回记录。我想要返回学生的分数,我会根据学生ID和班级ID找到结果。这是我的问题:

SELECT period_one.student_id, period_one.subject_id,  period_one.score, period_two.score,period_three.score, first_exam.score
        from period_one

LEFT JOIN period_two
ON period_one.subject_id = period_two.subject_id

LEFT JOIN period_three 
on period_one.subject_id = period_three.subject_id

LEFT JOIN first_exam 
on period_one.subject_id = first_exam.subject_id
WHERE period_one.student_id = 1 AND period_one.class_id = 2

我希望查询返回此结果: enter image description here 一切正常,只要表格中有一个学生ID(第一期,第二期和第三期),我就会得到我想要的结果。但是如果表格中有两(2)个学生ID(第一期,第二期和第三期),情况总是如此,那么它会返回。

enter image description here

我意识到获得我想要的结果的唯一方法就是更新我的where子句:

WHERE period_one.student_id = 1 AND period_one.class_id = 2
AND period_two.student_id = 1 AND period_two.class_id = 2
AND period_three.student_id = 1 AND period_three.class_id = 2

我认为这表明学生id和class id必须为三个表,如果不是,则返回没有结果。

即使它以某种方式确实如果我用上面的代码更新我的where子句,我也不想这样做,因为它会返回其他学生的科目成绩。有没有其他方法可以实现这一点而不像我那样修改我的where子句?让我知道我是否应该从我的查询中获取var dump()。

以下是我的表格:

table period_one {
    id int AUTO INCREMENT,
    student_id int, ## foreign key
    subject_id int, ## foreign key
    class_id int, ## foreign key
    score double
}

table period_two {
    id int AUTO INCREMENT,
    student_id int, ## foreign key
    subject_id int, ## foreign key
    class_id int, ## foreign key
    score double
}

table period_three {
    id int AUTO INCREMENT,
    student_id int, ## foreign key
    subject_id int, ## foreign key
    class_id int, ## foreign key
    score double
}

我不确定我是否在这里以正确的方式做到这一点,所以如果有更好的方法可以做到这一点,或者如果您需要更多信息,请告诉我。

2 个答案:

答案 0 :(得分:2)

您还需要加入FKs

SELECT period_one.student_id, period_one.subject_id,  period_one.score, 
       period_two.score,period_three.score, first_exam.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

LEFT JOIN first_exam ON 
             period_one.subject_id = first_exam.subject_id
             AND period_one.student_id = first_exam.student_id
WHERE period_one.student_id = 1 
AND period_one.class_id = 2

如果您希望获得class_id的正确结果,请考虑加入Class

SELECT period_one.student_id, period_one.subject_id,  period_one.score, 
       period_two.score,period_three.score, first_exam.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 
             AND period_one.class_id = period_two.class_id    
LEFT JOIN period_three ON 
             period_one.subject_id = period_three.subject_id 
             AND period_one.student_id = period_three.student_id
             AND period_one.class_id = period_three.class_id
LEFT JOIN first_exam ON 
             period_one.subject_id = first_exam.subject_id
             AND period_one.student_id = first_exam.student_id
             AND period_one.class_id = first_exam.class_id 
WHERE period_one.student_id = 1 
AND period_one.class_id = 2

答案 1 :(得分:0)

SELECT DISTINCT可能就是这样......

SELECT DISTINCT period_one.student_id, period_one.subject_id,  period_one.score, period_two.score,period_three.score, first_exam.score
        from period_one

LEFT JOIN period_two
ON period_one.subject_id = period_two.subject_id

LEFT JOIN period_three 
on period_one.subject_id = period_three.subject_id

LEFT JOIN first_exam 
on period_one.subject_id = first_exam.subject_id
WHERE period_one.student_id = 1 AND period_one.class_id = 2