如何修改此SQL连接以包括所有列?

时间:2013-08-07 08:46:16

标签: sql database join

我有这三个表进行一点测验。每个问题都有一个正确答案和三个错误答案

Table Name:            Columns:
Questions                   QuestionID, QuestionText, 
                            AnswerID (this stores id of correct answer)

Answers                     AnswerID, AnswerText, QuestionID
QuestionsAnswers            QuestionID,AnswerID

此查询

SELECT QuestionText, AnswerText
FROM [Questions] LEFT OUTER JOIN [Answers]
ON Questions.QuestionID=Answers.AnswerID;

给我以下结果

What is the capital of England? London
What is the capital of France?  Paris
What is the capital of USA?         Washington

我实际上还需要看到与每个问题分组的其他三个错误答案。 有点像

 What is the capital of England?    London
 What is the capital of England?    Berlin
 What is the capital of England?    BikiniBottom
 What is the capital of England?    Nottingham
 ... -- more results for France, USA and others follow

如何修改上面的查询以获得类似的结果?感谢

4 个答案:

答案 0 :(得分:3)

  

SELECT QuestionText,AnswerText   FROM [答案] LEFT OUTER JOIN [问题]   ON Answers.QuestionID = Questions.QuestionID;

答案 1 :(得分:2)

从上面的表结构中,

怎么样
SELECT QuestionText, AnswerText
FROM [Questions] LEFT OUTER JOIN [Answers]
ON Questions.QuestionID=Answers.QuestionID;

注意Answers.QuestionID而不是Answers.AnswerID

答案 2 :(得分:2)

试试这个

SELECT QuestionText, AnswerText
FROM [Questions] LEFT OUTER JOIN [Answers]
ON Questions.QuestionID=Answers.QuestionID;

答案 3 :(得分:1)

也许您必须加入QuestionsAnswers尝试以下解决方案

SELECT QuestionText, AnswerText
FROM [Questions] 
JOIN [QuestionsAnswers] ON QuestionsAnswers.QuestionID=QuestionsAnswers.AnswerID;
JOIN [Answers] ON QuestionsAnswers.AnswerID=Answers.AnswerID;