“问题”和“答案”;有多个答案
这个问题与这篇文章相关: 具有不同数据类型答案的调查的 SQL 设计
我有一个调查应用程序,其中大多数问题都有一组 1-5 的答案。现在我们必须做可能有多种不同答案类型的问题——数字、日期、字符串等。由于堆栈的建议,我使用了一个字符串列来存储答案。有些问题是多项选择,因此除了“问题”表之外,我还有一个“答案”表,其中包含问题的一组可能答案。
现在:我应该如何存储“选择所有适用的”问题的答案?我应该创建一个“chosen_answers”或类似的子表吗?或者答案表是否应该有一个“选择”列来表明受访者选择了该答案?
This question is related to this post:
SQL design for survey with answers of different data types
I have a survey app where most questions have a set of answers that are 1-5. Now we have to do questions that could have a variety of different answer types -- numeric, date, string, etc. Thanks to suggestions from stack, I went with a string column to store answers. Some questions are multiple choice, so along with the table 'questions', I have a table 'answers' which has the set of possible answers for a question.
Now: how should I store answers for a question that is "pick all that apply"? Should I make a child table that is "chosen_answers" or something like that? Or should the answers table have a 'chosen' column that indicates that a respondent chose that answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种可能的解决方案是具有 4 列的 UsersAnswers 表:主键、用户 id、问题 id 和答案 id,
对于可以选择多个答案的任何问题,具有多个条目
a possible solution is a UsersAnswers table with 4 columns: primary key, user's id, question's id, and answer's id
with multiple entries for any questions where more than one answer can be selected
我有两个建议。
标准化您的数据库,并创建一个名为
question_answer
的表,或者更符合您的架构命名法的表。这就是我的布局方式。在
answers
表中创建五列,每一列都引用一个特定答案。在 MySQL 中,我会将这些列设置为bit(1)
值。恕我直言,除非您看到选项数量发生变化,否则我会坚持使用选项 2。这是一个更快的选项,而且很可能还会节省您的空间。
I have two suggestions.
Normalize your database, and create a table called
question_answer
, or something that fits more in line with the nomenclature of your schema. This is how I would lay it out.Create five columns in your
answers
table, each of which refers to a specific answer. In MySQL I would use set these columns up asbit(1)
values.IMHO, unless you see the number of choices changing, I would stick with option 2. It's a faster option and will most likely also save you space.
由于您不会选择很多选项,因此我很想将答案存储为字符串答案列中以逗号分隔的值列表。
如果用户从带有问题的网页上的一组复选框中选择答案(假设它是一个网络应用程序),那么您也会从那里得到一个以逗号分隔的列表。 (尽管您不能仅将列表作为字符串进行比较,因为答案“红色,蓝色”与“蓝色,红色”相同。)
As you're not going to have many options selected I'd be tempted to store the answers as a comma-separated list of values in your string answer column.
If the user is selecting their answers from a group of checkboxes on the web page with the question (assuming it is a web app) then you'll get back a comma-separated list from there too. (Although you won't just be able to compare the lists as strings since the answer "red,blue" is the same as "blue,red".)
另一种选择(我也见过这样的问题是如何评分的),是将每个可能的答案视为一个单独的是/否问题,并记录测试者的回答(选择它,或者不选择它) ) 作为布尔值...
Another option, (And I've seen cases where this was how questions like this were scored as well), is to treat each possible answer as a separate Yes/No question, and record the testee's response (Chose it, or didn't) as a boolean...
这些调查问题始终有一个通用答案:这取决于您完成后想要如何处理答案。
例如,如果您只想保留每个人的记录答案(并且从不进行任何总计或查找用答案 y 回答问题 x 的所有用户),那么最简单的设计是将答案非规范化到序列化字段中。
如果您需要总计,如果您在汇总表中计算总计并在提交测验时更新值,则可能还可以将非规范化答案放入序列化表中。
因此,对于您的具体问题,您需要确定当您的意思是“以上所有”时,存储 5 对您的最终产品是否更有用,或者单独拥有四个选项中的每一个是否更有用已选择。
these survey questions always have one, universal answer: it depends on what you want to do with the answers when you're done.
for example, if all you want to to is keep a record of each individual answer (and never do any totaling or find all users that answered question x with answer y), then the simplest design is to denormalize the answers in to a serialized field.
if you need totals, you can probably also get away with denormalized answers in to a serialized table if you calculate the totals in a summary table and update the values when a quiz is submitted.
so for your specific question, you need to decide if it's more useful to your final product to store 5 when you mean "all of the above" or if it's more useful to have each of the four options individually selected.