Mysql连接字符串值位置-副

发布于 2024-12-10 18:47:48 字数 557 浏览 0 评论 0原文

我的问题有点难以解释,最简单的例子是:

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-12-17 18:47:48

正如上面评论中已经建议的那样,您确实应该将其重新设计为规范化结构!

不推荐以下内容:

如果您确实想要这样做使用您当前的数据模型,您可以执行类似于以下操作的操作(请注意,我不是 MySQL 专家,所以也许有更好的方法!):

SELECT s.StmNo, s.Statement, SUBSTR (a.Answer, s.StmNo, 1) 
FROM tblstm s CROSS JOIN tblanswer a WHERE a.RespondentId = 22 AND s.StmNo = 1
UNION
SELECT s.StmNo, s.Statement, SUBSTR (a.Answer, s.StmNo, 1) 
FROM tblstm s CROSS JOIN tblanswer a WHERE a.RespondentId = 22 AND s.StmNo = 2
UNION
SELECT s.StmNo, s.Statement, SUBSTR (a.Answer, s.StmNo, 1) 
FROM tblstm s CROSS JOIN tblanswer a WHERE a.RespondentId = 22 AND s.StmNo = 3

上面假设 StmNoAnswer 中的相应答案和为您提供前 3 个问题的答案...它既不高效也不优雅,并且可能有一种更优雅的方式使用 MySQL 来做到这一点(我从未使用过 MySQL)...

编辑 - 另一个选项:

创建一个表 tableAnswerNo,其中包含一列 AnswerNo、48 行...这些行包含来自 [1;48] 的每个不同数字。

然后您可以这样做:

SELECT s.StmNo, s.Statement, x.Answer FROM
tblstm s CROSS JOIN 
(SELECT a.RespondentId, an.AnswerNo, SUBSTR (a.Answer, an.AnswerNo, 1) Answer FROM
tblanswer a CROSS JOIN tableAnswerNo an WHERE a.RespondentId = 22) x ON x.AnswerNo = s.StmNo 
ORDER BY s.StmNo

这会按照您想要的方式为您提供 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!):

SELECT s.StmNo, s.Statement, SUBSTR (a.Answer, s.StmNo, 1) 
FROM tblstm s CROSS JOIN tblanswer a WHERE a.RespondentId = 22 AND s.StmNo = 1
UNION
SELECT s.StmNo, s.Statement, SUBSTR (a.Answer, s.StmNo, 1) 
FROM tblstm s CROSS JOIN tblanswer a WHERE a.RespondentId = 22 AND s.StmNo = 2
UNION
SELECT s.StmNo, s.Statement, SUBSTR (a.Answer, s.StmNo, 1) 
FROM tblstm s CROSS JOIN tblanswer a WHERE a.RespondentId = 22 AND s.StmNo = 3

The above assumes that StmNo is identical to the index of the respective answer in Answer 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 column AnswerNo with 48 rows... the rows contain each one distinct number from [1;48].

Then you could do this:

SELECT s.StmNo, s.Statement, x.Answer FROM
tblstm s CROSS JOIN 
(SELECT a.RespondentId, an.AnswerNo, SUBSTR (a.Answer, an.AnswerNo, 1) Answer FROM
tblanswer a CROSS JOIN tableAnswerNo an WHERE a.RespondentId = 22) x ON x.AnswerNo = s.StmNo 
ORDER BY s.StmNo

This gives you all the 48 answers for RespondentId 22 the way you want...

小姐丶请自重 2024-12-17 18:47:48

像这样的事情:

SELECT 
    s.StmNo
  , s.Statement
  , SUBSTRING(a.Answer, s.StmNo, 1) 
FROM
    tblstm s 
  JOIN
    tblanswer a 
      ON a.QType = s.QType 
WHERE
    a.RespondentId = @KnownRespondentId
ORDER BY 
    s.StmNo

Something like this:

SELECT 
    s.StmNo
  , s.Statement
  , SUBSTRING(a.Answer, s.StmNo, 1) 
FROM
    tblstm s 
  JOIN
    tblanswer a 
      ON a.QType = s.QType 
WHERE
    a.RespondentId = @KnownRespondentId
ORDER BY 
    s.StmNo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文