SQL subquery with two choices from another table gives random selection

SQL subquery with two choices from another table gives random selection

select top(1) Questions.Ques_ID, Question from Questions where Questions.Ques_ID in( select top(4) Answers.Que_ID,Answer from Answers where Questions.Ques_ID = Answers.Que_ID order by newid()) 

enter image description here

+6
source share
2 answers

When using IN() , the same number (and type) of elements should appear on both sides!

 select top(1) Questions.Ques_ID, Question from Questions where Questions.Ques_ID in( select top(4) Answers.Que_ID from Answers where Questions.Ques_ID = Answers.Que_ID order by newid()) 
+3
source

For me, it seems you want to get a random question with relevant answers?

 SELECT TOP 1 ques.Ques_ID, ques.Question, ans.Answer FROM Questions ques INNER JOIN Answers ans ON ans.ExamCode = ques.ExamCode AND ans.Que_Id = ques.Ques_ID ORDER BY NEWID() 

Is this what you want?

0
source

All Articles