我有 3 张桌子。A、B、C 如图所示。
Table A | Table B | Table C
---------------------- | ------------------------- |--------------------------------
StudentId StudentName | SubjectId SubjectName | StudentId SubjectId Marks
1 Jack | 101 History | 1 101 33
2 Peter | 102 Science | 2 102 75
3 Samantha | 103 Literature | 3 101 55
----------------------- | ------------------------- | -------------------------------
我需要一个查询来针对每个主题生成结果,如下所示:-
------------------------------------
StudentName SubjectName Marks
------------------------------------
Jack History 33
Jack Science 0
Jack Literature 0
Peter History 0
Peter Science 75
Peter Literature 0
Samantha History 33
Samantha Science 33
Samantha Literature 33
------------------------------------
我使用了以下未产生预期结果的查询。
1. select a.StudentName, b.SubjectName, c.Marks from a, b, c
where
a.StudentId = c.StudentId
and
c.SubjectId = b.StudentId
2. select a.StudentName, b.SubjectName, c.Marks from a, b, c
where
a.StudentId = c.StudentId
and
c.SubjectId = b.StudentId(+)
3.select a.StudentName, b.SubjectName, c.Marks from a, b, c
where
a.StudentId = c.StudentId
and
(+)c.SubjectId = b.StudentId
我的查询会错过分数不在表 C 中的科目。而我需要为每个学生重复表 b 中的所有三个科目。在输入的地方获得分数,如果没有针对 TableC 中的特定学生输入主题,则显示“0”。提前致谢。