2013-01-13 50 views
2

我有一個調查Web應用程序。調查可以有一個選擇題。多項選擇題的答案可以依賴於其他問題的答案。值的組合值 - 數據庫設計

實施例:

Question 1 has choices: HP, Acer, Samsung, Lenovo 
Question 2 has choices: Android, Ubuntu, iOS, Windows 
Question 3 has choices: Ubuntu, OS X, Windows 
Question 4 has choices: Adidas, Nike, Puma 

說問題4依賴於從問題1,2解答的組合和3

實施例1:

如果一個人的答案:問題1 =「HP」,問題2 =「Ubuntu」, 問題3 =「OS X」;問題4被自動設置爲 「彪馬」

實施例2:

如果一個人的答案:問題1 = 「宏」,問題2 = 「的Ubuntu」, 問題3 = 「Ubuntu的」 ;問題4自動設置爲「阿迪達斯」

*兩個示例都具有相同的邏輯。

一般來說,一些調查問題的答案可能依賴於其他一些調查問題的答案。

您如何爲此設計/建模數據庫?

這是我所創建的初始表關係(隨意修改):

Users: user_id 
Questions: question_id 
Choices: choice_id, question_id 
Answers: answer_id, user_id, question_id 

其他信息:

admin用戶界面的過程,我想這樣做的是:

1. The admin creates several independent questions (questions which have answers independent of other questions' answer) 
2. The admin creates a dependent question, selects one or many questions which he created earlier, selects a combination of answers from those question (just like in examples 1 and 2 above) and then sets an answer for the dependent question based on those combination of answers. 
... The admin proceeds creating several more questions. 

編輯:

感謝您的想法@MahmoudGamal。我創建了一個基於你的設計的東西:

Combinations table 
ID 
question_id # the dependent question's id 
choice_id # the automatic answer based on the combination of other answers 

Answer Combinations table 
ID 
combination_id 
question_id # the question that is depended upon by the dependent question 
choice_id # the choice that will be used for the combination 

所以我可以有一個問題的幾種組合。例如: 如果我想問題4接受幾種組合。一種組合有不同的答案。

Answer Combinations table 
ID  combination_id  question_id  choice_id 
1  1     1    1 
2  1     2    2 
3  1     3    3 
4  2     1    2 
5  2     2    2 
6  2     3    1 

及其組合表將具有

Combinations table 
ID  question_id  choice_id 
1  4    4 
2  4    3 

看起來相當整潔我。你怎麼看? PS:原諒我,但我是新來的堆棧溢出,我仍然找到我的方式。

+0

是的,當他們創建調查問題時,管理員將不得不手動設置這些關係。 –

+0

所以如果question1的答案是1,question2的答案是選擇2,question3的答案是選擇3,那麼question4的答案是選擇4.這是你想實現的邏輯嗎? –

+0

@MahmoudGamal不是。問題4可以是管理者選擇問題1,2和3的答案。沒有計算邏輯,只關聯邏輯(如果這是正確的術語)。 –

回答

0

因此,這裏是我可以從你的問題了解的情況:

管理規定,以前的問題的答案與另一個問題之間的關係,在此基礎上的條件這個問題將有一個自動應答在此基礎上關係,並且這個邏輯關係是手動指定的。因此,對於這一點,你需要兩個表:

PreviousQuestionsAnswers

  • QuestionId
  • PreviousQuestionId
  • PreviousAnswerID

QuestionPreAnswer

  • QuestionId
  • AnswerId

例如:

如果一個人的答案:問題1 = 「HP」,問題2 = 「的Ubuntu」, 問題3 = 「OS X」;問題4被自動設置爲 「美洲豹」

那麼這兩個表將有類似:

PreviousQuestionsAnswers

QuestionId PreviousQuestionId PreviousAnswerId 
    4    1     1 
    4    2     2 
    4    3     3 

QuestionPreAnswer

QuestionId AnswerId 
    4   4 

需要注意的是:這些數據由管理員預先輸入,因此,在OP應接受調查後的前端應用程序中,您將匹配他在前面提出的問題中輸入的答案,這些問題應該已在Answers中輸入,並且在PreviousQuestionsAnswers中有預先定義的條件表,如果是這樣的話,請選擇QuestionPreAnswer

+0

而不是評論,我編輯我的帖子,因爲評論只允許有限數量的字符。如果您檢查我的文章的「編輯」部分,我將不勝感激。 再次感謝! –

+0

@ SandyandiN.delaCruz這個更好,這個'combinationid'可以讓你爲每個'questionid'做出許多組合。 –

+0

在那裏,我檢查了它。非常感謝! –

0

原諒我的英語。 :)我的想法也是...你將不得不爲prereq答案創建新表。

PreAns表樣品

q_id = 2 
pre_qid = 1 
pre_ans = 3 
your_ans = 2 

然後檢查PreAns表是否已被正確預答案回答。

只是想通過:)希望將有所幫助