Mysql连接字符串值位置-副
我的问题有点难以解释,最简单的例子是:
在 mysql 中,我有两个表,一个包含问卷中的陈述,一个包含答案,如下所示:
第一个表:
tblstm
StmNo, Statement, LanguageISO3
陈述是制定的问题在LanguageISO3中,即(1,“问题1”,“ENU”),(2,“问题2”,“ENU”)等...
第二个表:
tblanswer
RespondentId, Answer
答案是一个字符串0 或 1 为是或否,即。例如“01010110110111”,该位的位置对应于tblstm中的StmNo。
如何编写查询,以便针对已知的 RespondentId,在单独的行(如 tblstm 表)中获取每个语句以及相应的答案。结果应该是这样的:
1, Question 1, 0
2, Question 2, 1
3, Question 3, 0
etc
My problem is a bit difficult to explain, easiest by example:
In mysql I have two tables, one with statments from a questionaire and one with answers, something like this:
First table:
tblstm
StmNo, Statement, LanguageISO3
Statment is the formulated question in LanguageISO3, i.e. (1, "Question 1", "ENU"), (2, "Question 2", "ENU"), etc...
The second table:
tblanswer
RespondentId, Answer
Answer is a string with 0 or 1 for yes or no, ie. "01010110110111" for example, the positon of the bit is corresponding to a StmNo in tblstm.
How do I write a query so I get each statment in a seperate row (like tblstm table) with the corresponding answer, for a known RespondentId. The result should be something like this:
1, Question 1, 0
2, Question 2, 1
3, Question 3, 0
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如上面评论中已经建议的那样,您确实应该将其重新设计为规范化结构!
不推荐以下内容:
如果您确实想要这样做使用您当前的数据模型,您可以执行类似于以下操作的操作(请注意,我不是 MySQL 专家,所以也许有更好的方法!):
上面假设
StmNo
与Answer
中的相应答案和为您提供前 3 个问题的答案...它既不高效也不优雅,并且可能有一种更优雅的方式使用 MySQL 来做到这一点(我从未使用过 MySQL)...编辑 - 另一个选项:
创建一个表
tableAnswerNo
,其中包含一列AnswerNo
、48 行...这些行包含来自 [1;48] 的每个不同数字。然后您可以这样做:
这会按照您想要的方式为您提供
RespondentId
22 的所有 48 个答案...As already advised in the comments above you should really redesign that to a normalized structure!
The following is NOT recommended:
IF you really want to do this with your current datamodel you can do it similar to the following (please note I am no MySQL expert so perhaps there is a better way!):
The above assumes that
StmNo
is identical to the index of the respective answer inAnswer
and gives you the answers for the first 3 questions... it is NOT efficient nor elegant AND there may be a more elegant way to do this with MySQL (I never used MySQL)...EDIT - another option:
Create a table
tableAnswerNo
with one columnAnswerNo
with 48 rows... the rows contain each one distinct number from [1;48].Then you could do this:
This gives you all the 48 answers for
RespondentId
22 the way you want...像这样的事情:
Something like this: