0

I'm facing a SQL SELECT problem:

There is a question, this question has an anwser, and this answer has a next question, and so on... This is my query:

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;

This is the output of the above query:

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

So right now the 'next question' is a number... But I'd like to resolve that number to the next question text (Q.question)... How can I achieve this?

I want something like this:

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

Thanks in advance!!

I'm working with MySQL.

This is my table structure:

Table "Questions"

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

Table "Answers"

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

1 に答える 1

0

データ構造に、質問から回答へのリンクがありません。

したがって、 にはと同様にanswersフィールドがあると仮定します。これは、必要なリンクを取得する 1 つの方法です。そのようなリンクがない場合は、データ構造についてもう少し考える必要があります。QuestionIdNextQuestionId

このようなフィールドでは、questions必要なものを取得するための 2 番目の結合が行われます。

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 に答える