简体   繁体   English

我的返回结果是返回除一个之外的所有正确答案,没有错误

[英]My return-result is returning all the correct answers except one, with not errors

This is a homework question.这是一道作业题。 I am not asking for the correct answer, I am just looking for help whether I am going in the right direction.我不是在寻求正确的答案,我只是在寻求帮助,看看我是否朝着正确的方向前进。

The prompt is that I am to return the first and last name of students who either (OR): (a) Is not currently enrolled (not in Enrollments table) or (b) has the lowest score in any class within their own department.提示是我要返回以下学生的名字和姓氏:(或):(a)当前未注册(不在注册表中)或(b)在其所在部门的任何班级中得分最低。

The database schema is as follows:数据库架构如下: 第1部分 第2部分

My SQL query that I came up with is:我想出的 SQL 查询是:

SELECT FIRSTNAME, LASTNAME
FROM STUDENTS
JOIN ENROLLMENTS ON STUDENTS.NETID = ENROLLMENTS.NETID
JOIN COURSES ON COURSES.CRN = ENROLLMENTS.CRN
WHERE STUDENTS.NETID NOT IN (
  SELECT NETID  
  FROM ENROLLMENTS  
) OR
ENROLLMENTS.SCORE IN (
  SELECT MIN(SCORE)  
  FROM ENROLLMENTS  
  WHERE COURSES.DEPARTMENT = STUDENTS.DEPARTMENT  
  GROUP BY ENROLLMENTS.CRN  
);

I tried to use JOIN clauses to combine the three tables where they intersect.我尝试使用 JOIN 子句来组合它们相交的三个表。 The NetId is distinct to each student, identifying them in the Enrollments table.每个学生的 NetId 都是不同的,在 Enrollments 表中标识他们。 Each class has a unique CRN so I connected them there between Courses table and Enrollments table.每个班级都有一个唯一的 CRN,因此我将它们连接到 Courses 表和 Enrollments 表之间。

I do not know where the problem lies but the expected outcome is:我不知道问题出在哪里,但预期的结果是:

Wbixik Yjepuriluwe维比西克·耶普里鲁韦

Wtoyi Avamijosu Wtoyi Avamijosu

Jheyiresoxo Bsexedoh Jheyiresoxo Bsexedoh

Ulerusota Mzuzu乌勒鲁索塔·姆祖祖

But my outcome is:但我的结果是:

Wbixik Yjepuriluwe维比西克·耶普里鲁韦

Jropop Vduyumi Jropop Vduyumi

Jheyiresoxo Bsexedoh Jheyiresoxo Bsexedoh

Ulerusota Mzuzu乌勒鲁索塔·姆祖祖

Looking for any guidance to get me back on track.寻找任何指导让我回到正轨。

You can try using left join您可以尝试使用左连接

SELECT FIRSTNAME, LASTNAME
FROM STUDENTS
left JOIN ENROLLMENTS ON STUDENTS.NETID = ENROLLMENTS.NETID
JOIN COURSES ON COURSES.CRN = ENROLLMENTS.CRN
where ENROLLMENTS.NETID is null OR
ENROLLMENTS.SCORE IN 
    (SELECT MIN(SCORE) FROM ENROLLMENTS
        WHERE COURSES.DEPARTMENT = STUDENTS.DEPARTMENT
        GROUP BY ENROLLMENTS.CRN
    )

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

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