SQL学生标记查询

时间:2014-10-17 08:40:29

标签: sql tsql sql-server-2012

如何选择未在某些子课程中标记的学生的分数

edu_submark:
Id      sm_mark sub_lesson_id    st_id  course_id
1       97       1               9      3
2       66       2               9      3
3       22       2               1012   3
4       32       1               1012   3
1002    15       1               13     3

edu_sub_lesson:
Id      sl_title            lesson_id
1       Active Directory        2
2       Win7                    2
2009    UI                      1
2011    Win SERVER 2008 R2      2

我使用了这个查询:

SELECT 
    a.sl_title, b.sm_mark 
FROM  
    edu_sublesson a 
LEFT JOIN
    edu_submark b on a.Id = b.sub_lesson_id 
WHERE
    lesson_id = 2 AND course_id = 3 AND st_id = 9

结果:

    Active Directory 99.9
    Win 7            75

但我想要这个结果:

    Active Directory 99.9
    Win Server       NULL
    Win 7            75
  • sub_lesson有一个课程表的外键,课程也有一个课程表的外键。

3 个答案:

答案 0 :(得分:0)

如果从

更改WHERE子句
WHERE
    lesson_id = 2 AND course_id = 3 AND st_id = 9

到这个

WHERE
    -- Restrict to specific lesson/course etc..
    (lesson_id = 2 AND course_id = 3 AND st_id = 9)
    -- return any which don't have marks
    OR sm_mark IS NULL

答案 1 :(得分:0)

尝试将您的查询更改为

SELECT 
    a.sl_title, b.sm_mark 
FROM  
    edu_sublesson a 
LEFT JOIN
    edu_submark b on a.Id = b.sub_lesson_id AND b.course_id = 3 AND b.st_id = 9
WHERE
    a.lesson_id = 2

答案 2 :(得分:-1)

好吧也许你应该这样做:

SELECT 
    a.sl_title, b.sm_mark, WinServer, Administration

或者这个字段有什么名字..