2017-04-23 74 views
0

免责声明:很抱歉,如果这是一个简单的问题,或措辞严厉的问题。我是一个noob,我不知道怎么说这个问题,所以我不知道如何谷歌它。子查询的列数太多;多个选择在一个选择语句

我有两个表。 Math.question和Math.choice。

Math.question包含一个questiontext列,以及correctanswerid,wronganswerid1 ...等等。

questiontext   correctanswerid  wronganswerID1 wronganswerid2 
(question text   101     102    103 
    goes here) 

Math.choice包含选择的ID和选择文本。

id  choicetext 
101 (text goes here) 
102 (text goes here) 
... 

我想从math.question和choicetext中选择questiontext。所以它会显示是这样的:

questiontext  correctanswerText wronganswer1text wronganswer2text 
(question text) (choice text)  (choice text)  (choice text) 

我已经试过这样:

select * from math.choice 
where id in (
    select CorrectAnswer_Choice_ID, Foil1_Choice_ID, Foil2_Choice_ID, Foil3_Choice_ID 
    from math.question where id=301 
); 

我得到的子查询有太多的列错误。我不确定该从哪里出发。

回答

0

您需要多个join s。类似这样的:

select c.*, q1. choicetext, q2.choicetext, q3.choicetext 
from math.choice c left join 
    math.question q1 
    on q1.id = c.CorrectAnswer_Choice_ID left join 
    math.question q2 
    on q2.id = c.Foil1_Choice_ID left join 
    math.question q3 
    on q3.id = c.Foil3_Choice_ID ; 
+0

不幸的是我不在家,所以我不能试试这个。当我回家时,我会将此标记为已回答:)。但这看起来应该起作用。出于某种原因,我不认为你可以多次加入同一张桌子。谢谢! –