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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql- SELECT列'A',即使使用NULLS相关的知识,希望对你有一定的参考价值。

表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 | | | | | | | 等...

答案

您应该将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 

以上是关于Mysql- SELECT列'A',即使使用NULLS的主要内容,如果未能解决你的问题,请参考以下文章

mysql在查询结果中自动添加序号字段列

mysql在查询结果中自动添加序号字段列

oracle查询语句 select a','b','c from table where a in('m','n')

<mysql问题>如何同时使用between和like吗?

mysql多表查询合并到一个临时表,怎么再加一列并把各自的表名加上?

如何在mysql中查询所有表是不是包含某个字段