简体   繁体   中英

SQL Subquery with two selects from different table is giving random selection

SQL Subquery with two selects from different table is giving 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())

在此输入图像描述

When using IN() , same amount(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())

For me, it seems like you want to get a random Question with Matching 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM