我读了很多页但没找到我要找的东西。 我试图找出哪个是我的问题最合适的解决方案。我目前有一个包含23个问题和23个评论部分的多项选择表格(每个650个字符,基本上是每个问题的评论),我不确定是否应该使用单个表格,或者是否会过多。
所以基本上是这样的:
id
user_id
date
multiplechoice1
..
multiplechoice23
comment1
..
comment23
status
这将是大约50列:/有更好的方法来做到这一点?就像在不同的表上拆分注释或以某种方式将所有多个选项组合在一个列中?由于每个答案都是1-5。
问题的例子:
答案 0 :(得分:0)
考虑到你的意见,因为你不想太过正常化并且喜欢略微扁平的东西,我认为这对你来说可能是一个合适的解决方案。
questions
+----+-------------------------------+
| id | question |
+----+-------------------------------+
| 1 | What is your favorite food? |
+----+-------------------------------+
| 2 | What is your favorite animal? |
+----+-------------------------------+
answers
- 问题1有三个答案,问题2有两个答案
+----+-------------+-----------+
| id | question_id | answer |
+----+-------------+-----------+
| 1 | 1 | Spaghetti |
+----+-------------+-----------+
| 2 | 1 | Chicken |
+----+-------------+-----------+
| 3 | 1 | Sushi |
+----+-------------+-----------+
| 4 | 2 | Dog |
+----+-------------+-----------+
| 5 | 2 | Cat |
+----+-------------+-----------+
answer_data
- 一个人回答问题1的“意大利面”,一个人回答问题2的“猫”,一个人回答问题2的“狗”
+----+-------------+-----------+
| id | question_id | answer_id |
+----+-------------+-----------+
| 1 | 1 | 1 |
+----+-------------+-----------+
| 2 | 2 | 4 |
+----+-------------+-----------+
| 3 | 2 | 5 |
+----+-------------+-----------+
说实话,我对MySQL不太好,我是一个T-SQL人,但查询可能看起来像这样:
SELECT q.question, a.answer, COUNT(ad.answer_id) as `Count`
FROM questions q
JOIN answers a ON a.question_id = q.question_id
JOIN answer_data ad ON ad.question_id = q.question_id
GROUP BY q.question, a.answer
+-------------------------------+-----------+-------+
| question | answer | count |
+-------------------------------+-----------+-------+
| What is your favorite food? | Spaghetti | 1 |
+-------------------------------+-----------+-------+
| What is your favorite animal? | Dog | 1 |
+-------------------------------+-----------+-------+
| What is your favorite animal? | Cat | 1 |
+-------------------------------+-----------+-------+