SQL correct way of joining if the other parameter is null

后端 未结 5 1423
离开以前
离开以前 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:41

    This

    Select Q.id , Q.quiz_name ,S.firstname, S.lastname
    from 
        #quiz Q                -- cross join, returns N*K results, do not use without 
        CROSS JOIN #student S  -- where condition that limits it - SAS solution is nicer!
    where not exists (select 1 from #quiz_details where quiz_id = Q.id and student_id = S.id)
    

    will give you

    id  quiz_name                         firstname     lastname
    1   NBA 50 Greatest Player Quiz       LeBron        James
    2   NBA Top 10 3 point shooters       Stephen       Curry
    

    Edit: changed code to explicit cross join rather then implicit, leaving both in here for comparison

    SELECT #quiz Q, # student S           -- old implicit syntax - comma is easily missed
    

    vs.

    SELECT #quiz Q CROSS JOIN #student S  -- makes it clearer what is wanted
    
    0 讨论(0)
  • 2021-02-02 06:42

    Pretty sure you want something along these lines. This will give you the quiz values and return NULL for the student and quiz_details when there is no matching data.

    select *
    from #quiz q
    left join #quiz_details qd on q.id = qd.quiz_id
    left join #student s on s.id = qd.student_id
    
    0 讨论(0)
  • 2021-02-02 06:44

    My take on it - similar to Patrick's answer with a cross join.

    Full sample available at sqlfiddle

    select
      Q.Quiz_Name Quiz
     ,S.LastName Last
     ,S.FirstName First
     ,QD.Quiz_ID
     ,QD.Student_ID
    from 
    /* Get a full list of ALL Test/Student combinations */
               quiz Q 
    CROSS JOIN student S 
    /* Join the taken tests to the combinations */
     LEFT JOIN quiz_details QD on Q.id = QD.quiz_id
                              and S.id = QD.student_id
    /* Only select where no Quiz_ID exists */
    WHERE QD.Quiz_ID IS NULL
    ORDER BY Q.Quiz_Name, S.Lastname, S.FirstName;
    
    0 讨论(0)
  • 2021-02-02 06:52
    select s.firstname, s.lastname, q.id as not_taken_quiz_id, q.quiz_name as not_taken_quiz_name
    from #student s
    left join #quiz_details qd on s.id = qd.student_id
    left join #quiz q on q.id <> qd.quiz_id
    

    This will give you each student along with the quiz that they have not taken.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题