SQL correct way of joining if the other parameter is null

后端 未结 5 1424
离开以前
离开以前 2021-02-02 06:07

I have this code and its temporary tables so you can run it.

create table #student
(
    id int identity(1,1),
    firstname varchar(50),
    lastname varchar(50         


        
5条回答
  •  被撕碎了的回忆
    2021-02-02 06:57

    This should get you started:

    -- filter out the student and quiz you want
    DECLARE @qid INT = 1
    DECLARE @sid INT = 1
    
    SELECT * 
    FROM #student AS s
    INNER JOIN #quiz AS q  -- you want the quiz
     ON 1=1
    LEFT OUTER JOIN #quiz_details AS qd  -- left join here to get result where rows not found
     ON qd.id = q.id 
     AND qd.student_id=s.id
    WHERE s.id = @sid
     AND q.id = @qid
     AND qd.id IS NULL -- only return quizes not taken
    

提交回复
热议问题