簡體   English   中英

SQL學生打分查詢

[英]SQL students mark query

如何選擇未在某些子課程中打分的學生的打分

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有課程表的外鍵,而課程也有課程表的外鍵。

如果您將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

嘗試將查詢更改為

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

好吧,也許您應該這樣做:

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

或任何具有此字段的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM