如何构建调查数据?
注意:这不像这个问题,
我的任务是构建一个我们的客户服务人员可以使用该调查从客户那里获取有关我们服务等的信息。
我的问题是如何将问题/答案存储在存储问题的数据库中,如下所示:
- 布尔问题1
- if (false): string Question2(通常是 Question1 为 false 的原因)
这很容易,但是问题可以嵌套多个级别:
- boolean Question1
- if (false) 布尔问题2
- if (true) 字符串问题3
- 如果(假)字符串问题4
- if (false) 布尔问题2
如果我不必将其存储在数据库中,我会将问题(实际上是整个调查)表示为一个复合体,但我不这样做知道我会如何存储这样的东西。
Note: this is NOT like this question
I've been tasked to construct a survey that our customer service people can use to get info from our customers about our service etc.
My question is how to store the questions/answers in the db where the questions are stored like so:
- boolean question1
- if (false): string question2 (typically the reason why question1 was false)
This would be easy, however the questions can be nested multiple levels:
- boolean question1
- if (false) boolean question2
- if (true) string question3
- if (false) string question4
- if (false) boolean question2
If I didn't have to store it in the db, I would represent the questions (and indeed the whole survey) as a composite, but I don't know how I'd store something like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的问题形成一棵树,而不是一个图表,有两种常见的方法表示sql中的分层数据。
邻接列表模型需要重复的自连接来查找子项(子项的...子项的)。 如果您有一个 ORM,例如 Hibernate,ORM 将负责执行足够的自连接来带回问题及其子答案。 如果没有 ORM 来执行此操作,您将必须动态地将自联接添加到查询中,或者执行多个查询,每个查询对应上一个结果集中的每一行。
嵌套集模型通常归功于 Joe Celko,它允许您在一次选择中获得一整棵树。 但这意味着添加和删除节点更加复杂,需要更新所有行。
在邻接列表中,您可以有一个像这样的问题表
(id, Question text, id_next_if_true, id_next_if_false)
。 这与经典的邻接列表不同,因为父级不是持有parent_id,而是持有两个子级id,或者为null。If your questions form a tree, and not a graph, there are two common ways to represent hierarchical data in sql.
The Adjacency List Model model requires repeated self-joins to look up children (of children ... of children). If you have an ORM, like Hibernate, the ORM will take care of doing sufficient self-joins to bring back a question and its child answers. Without an ORM to do that, you'll have to either dynamically add self-joins to a query, or do multiple queries, one for each row in the previous result set.
The Nested Set Model, usually attributed to Joe Celko, allows you to get a whole tree in a single select. But it means that adding and deleting nodes is more complicated, requiring updates to all rows.
In Adjacency List, you could have a question table like this
(id, question text, id_next_if_true, id_next_if_false)
. This departs from the classical Adjacency List, in that instead of children holding the parent_id, the parent holds two children ids, or null.我想我会做这样的事情:
如果问题是多项选择,你可以使用 int、char 或 string 来代替 bool。
I think I would do something like this:
Instead of a bool, you could use an int, char, or string for your answer if the questions are multiple choice.