Mysql- SELECT列'A',即使使用NULLS

问题描述 投票:0回答:1

表A包含学生姓名,表B和C包含班级和学生的存在。我想展示所有学生并参加他们的出席。问题是我无法显示所有没有检查过的学生。在我检查学生在场的情况下,这是可以的,但如果在给定的课程中,在给定的一天和给定的科目中没有检查过的存在 - 则不显示任何内容。

我的查询:

SELECT student.id_student, CONCAT(student.name,' ' ,student.surname) as 'name_surname',pres_student_present, pres_student_absent, pres_student_justified, pres_student_late, pres_student_rel, pres_student_course, pres_student_delegation, pres_student_note FROM student
    LEFT JOIN class ON student.no_classes = class.no_classes
    LEFT JOIN pres_student ON student.id_student = pres_student.id_student
    WHERE (class.no_classes = '$class' OR NULL AND pres_student_data = '$data' AND pres_student_id_subject = $id_subject OR NULL)
    GROUP BY student.surname
    ORDER BY student.surname ASC

我想始终显示name_surname,任何其他列应该有NULL或1像: Name | present | absent | just | late | rel | delegation | note | Donald Trump | 1 | | | | | | | Bush | | | | | | | | Someone | 1 | | | | | | | 等...

mysqli
1个回答
1
投票

您应该将classpres_studenttables的限制从WHERE子句移到ON(LEFT join)。

在你使用WHEREouter join子句执行限制的情况下,sql引擎认为你正在执行INNER join

SELECT    student.id_student
        , CONCAT(student.name, ' ', student.surname) AS 'name_surname'
        , pres_student_present
        , pres_student_absent
        , pres_student_justified
        , pres_student_late
        , pres_student_rel
        , pres_student_course
        , pres_student_delegation
        , pres_student_note
FROM      student
LEFT JOIN class
       ON student.no_classes = class.no_classes
      AND class.no_classes   = '$class'
LEFT JOIN pres_student
       ON student.id_student      = pres_student.id_student
      AND pres_student_data       = '$data'
      AND pres_student_id_subject = $id_subject
GROUP     BY student.surname
ORDER     BY student.surname ASC 
© www.soinside.com 2019 - 2024. All rights reserved.