0

我面临一个 SQL SELECT 问题:

有一个问题,这个问题有一个答案,这个答案有一个下一个问题,依此类推......这是我的查询:

SELECT Q.question, A.answer, A.nextQuestion as 'next question' from answers A join questions Q on A.nextQuestion = V.questionId 
order by Q.question;

这是上述查询的输出:

Question 1 | Answer 1 | 2
Question 1 | Answer 2 | 2
Question 2 | Answer 1 | 1
Question 2 | Answer 2 | 1

所以现在“下一个问题”是一个数字......但我想将该数字解析为下一个问题文本(Q.question)......我怎样才能做到这一点?

我想要这样的东西:

Question 1 | Answer 1 | Question 2
Question 1 | Answer 2 | Question 2
Question 2 | Answer 1 | Question 1
Question 2 | Answer 2 | Question 1

提前致谢!!

我正在使用 MySQL。

这是我的表结构:

表“问题”

+------------------+
| Field            |
+------------------+
| questionId       |
| question         |
+------------------+

表“答案”

+------------------+
| Field            |
+------------------+
| answerId         |
| answer           |
| nextQuestion     |
+------------------+
4

1 回答 1

0

您的数据结构缺少从问题到答案的链接。

所以,我要假设answershas a fieldQuestionId以及NextQuestionId. 这是您获得所需链接的一种方式。如果您没有这样的链接,那么您需要更多地考虑您的数据结构。

有了这样一个字段,第二次加入就questions可以得到你需要的东西:

SELECT Q.question, A.answer, nextq.Question as 'next question'
from questions Q
     answers A join
     on A.QuestionId = q.questionId join
     questions nextq
     on A.NextQuestionId = nextq.QuestionId
order by Q.question;
于 2013-05-11T19:19:41.127 回答