如何在连接多个表时返回NULL值

时间:2016-04-15 00:08:29

标签: mysql sql

这是表格的内容。

mysql> desc student;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| name       | varchar(20)      | NO   |     | NULL    |                |
| sex        | enum('F','M')    | NO   |     | NULL    |                |
| student_id | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
+------------+------------------+------+-----+---------+----------------+

mysql> desc grade_event;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| date     | date             | NO   |     | NULL    |                |
| category | enum('T','Q')    | NO   |     | NULL    |                |
| event_id | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
+----------+------------------+------+-----+---------+----------------+

mysql> desc score;
+------------+------------------+------+-----+---------+-------+
| Field      | Type             | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| student_id | int(10) unsigned | NO   | PRI | NULL    |       |
| event_id   | int(10) unsigned | NO   | PRI | NULL    |       |
| score      | int(11)          | NO   |     | NULL    |       |
+------------+------------------+------+-----+---------+-------+

我想要完成的是显示哪些学生错过了一个测验/测试,发现在'类别'在grade_event表中。

这是我提出的,但没有产生任何结果;

select name, category, sc.event_id
from student s
join score sc on s.student_id=sc.student_id
join grade_event ge on sc.event_id=ge.event_id
where score is NULL
group by name, event_id;

我也走了尝试子查询的路线;

select name, category, sc.event_id
from student s
join score sc on s.student_id=sc.student_id
join grade_event ge on sc.event_id=ge.event_id
where score not in (select score from score)
group by name, event_id;

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

我认为您应该在Time.current.between?('09:00 UTC-05:00', '19:00 UTC-05:00') 之前替换joinleft join是MySQL中的joinhttp://dev.mysql.com/doc/refman/5.7/en/join.html

并注意inner join,这对精确group by event_id有用。我不知道在MySQL中,但在sql server中它不起作用。

当您要求某个列的值不在其值中时,您的第二个查询必须为空:)

答案 1 :(得分:0)

您需要使用外部联接而不是内部联接来获取在创建学生和事件的联合会加入后事件中没有相应记录的学生列表:

JSchema aSchema;

object foo = new {propA = "x", schema =  aSchema};