所以我有这些让我有点头疼的表格。我想要做的是创建一个查询,返回给定课程的所有作业以及作业信息(成绩、平均成绩等),或者如果学生没有提交或没有提交作业,则在这些字段中返回 null . 我已经尝试过left join
,但我无法为这些表弄清楚。它们很旧而且结构很差,但这是我所拥有的。
CourseInst_Students在课程实例中为学生保存注册
| ID_CourseInst| SSN |
| =============|=====================|
| 24744 | 080590-XXXX | my ssn
Assignments_CourseInst将作业注册到课程实例
| ID_Assignment| ID_CourseInst |
| =============|=====================|
| 37978 | 24744 |
| 37979 | 24744 |
| 37992 | 24744 |
| 38046 | 24744 |
作业
| ID_Assignment| Title |
| =============|===========================|
| 37978 | Og þá var kátt í höllinni |
| 37979 | Test for Assignments |
| 37992 | Test 2 |
| 38046 | Eitthvað gott verkefni |
Assignments_Solutions保存一组学生(一对多)的信息(成绩、提交日期等)。如果学生没有提交他们的解决方案,则没有记录。
| ID_Assignment| ID_Group | Grade |
| =============|===========================|=========|
| 37978 | 808046 | 10 | only one group has handed in.
Assignments_Solutions_Groups将学生的 SSN 注册到组。
| ID_Group | SSN |
| =============|===========================|
| 808046 | 221180-XXXX | not my SSN (see mine above)
在为我的 SSN 搜索作业时,我的首选结果类似于:
| ID_Assignment| Title | Grade |
| =============|===========================|=========|
| 37978 | Og þá var kátt í höllinni | NULL |
| 37979 | Test for Assignments | NULL |
| 37992 | Test 2 | NULL |
| 38046 | Eitthvað gott verkefni | NULL |
任何人都可以解决这个问题吗?谢谢!
更新
这是我到目前为止所得到的
select A.ID_Assignment, A.Title, ASo.Grade
from CourseInst_Students as CS
join Assignments_CourseInst as AC on CS.ID_CourseInst= AC.ID_CourseInst
join Assignments as A on AC.ID_Assignment = A.ID_Assignment
left join Assignments_Solutions as ASo on A.ID_Assignment = ASo.ID_Assignment
left join Assignments_Solutions_Groups as ASGs on ASo.ID_Group = ASGs.ID_Group
where CS.SSN = '080590-XXXX'
AND CS.id_namskeid = 24744
我得到以下结果:
| ID_Assignment| Title | Grade |
| =============|===========================|=========|
| 37978 | Og þá var kátt í höllinni | 10 | <- this is someone else's handin. I want NULL here
| 37979 | Test for Assignments | NULL |
| 37992 | Test 2 | NULL |
| 38046 | Eitthvað gott verkefni | NULL |