简体   繁体   English

从一个列中加入多个值,从另一个表中选择

[英]Join multiple values from one column, selected from another table

Given these simplified multiple choice tables where sometimes more than one answer is correct: 鉴于这些简化的多选表有时不止一个答案是正确的:

STUDENT_ANSWERS
AnswerID | StudentID | QuestionID | Answers
-------------------------------------------
       1 |         1 |          1 | C,D

QUESTION_ANSWERS
QuestionID | Answer | Text
-------------------------------------------------
         1 |      A | This is answer A
         1 |      B | B could also be correct
         1 |      C | Maybe it's C?
         1 |      D | Definitely D!

How do I do a select which translates the answers to their descriptions? 如何进行选择以转换其描述的答案?

My start: 我的开始:

SELECT *
FROM STUDENT_ANSWERS sa
LEFT OUTER JOIN QUESTION_ANSWERS qa ON qa.Answer IN sa.Answers???
 -- Doesn't seem to work as IN requires a format of ('C','D') while I have 'C,D'

Desired output: 期望的输出:

AnswerID | StudentID | QuestionID | AnswerDescriptions
-------------------------------------------
       1 |         1 |          1 | Maybe it's C?,Definitely D!

So the descriptions simply have to replace the codes instead of getting a single line for each answer. 因此,描述只需替换代码而不是为每个答案获取单行。

Your problem is the structure of table STUDENT_ANSWERS . 你的问题是表STUDENT_ANSWERS的结构。 It should have one row per answer: 每个答案应该有一行:

AnswerID | StudentID | QuestionID | Answer
-------------------------------------------
       1 |         1 |          1 | C
       2 |         1 |          1 | D

Now, assuming you can't do anything to change (read: fix) this, you can fudge it with appending a comma and using LIKE: 现在,假设你无法做任何改变(阅读:修复)这个,你可以通过附加逗号和使用LIKE来捏造它:

select *
from STUDENT_ANSWERS a
join QUESTION_ANSWERS q on ',' + a.Answers + ',' like '%,' + q.Answer + ',%'
    and a.QuestionID = q.QuestionID

SQL Fiddle demo SQL小提琴演示

Note this assumes you will never ever have the text , in QUESTION_ANSWERS.Answer . 请注意这是假设你永远不会有文本,QUESTION_ANSWERS.Answer It will also never be able to use an index, so it's going to be slower than slow. 它也永远不能使用索引,所以它会比慢速慢。

And if you absolutely must format this in the database to be one line, you can use STUFF and the FOR XML PATH('') trick to concatenate the resulting rows. 如果您绝对必须将数据库中的格式设置为一行,则可以使用STUFFFOR XML PATH('')技巧来连接生成的行。

This is full working example using only T-SQL statements. 这是仅使用T-SQL语句的完整工作示例。 I will recommend to you to create separate function for splitting CSV that returns row set. 我建议您创建单独的函数来拆分返回行集的CSV Also, if you are working with huge amount of data, you may want to create a CLR function for splitting the values. 此外,如果您正在处理大量数据,则可能需要创建用于拆分值的CLR函数。 Take a look to this article (there is everything you need). 看看这篇文章 (你需要的一切)。

DECLARE @StudentAnswers TABLE
(
     [AnswerID] INT
    ,[StudentID] INT
    ,[QuestionID] INT
    ,[Answers] VARCHAR(256)
);

DECLARE @QuestionAnswers TABLE
(
     [QuestionID] INT
    ,[Answer] CHAR
    ,[Text] VARCHAR(256)
);

INSERT INTO @StudentAnswers ([AnswerID], [StudentID], [QuestionID], [Answers])
VALUES (1, 1, 1, 'C,D')
      ,(2, 2, 1, 'A');

 INSERT INTO @QuestionAnswers ([QuestionID], [Answer], [Text])
 VALUES  (1, 'A', 'This is answer A')
        ,(1, 'B', 'B could also be correct')
        ,(1, 'C', 'Maybe it''s C?')
        ,(1, 'D', 'Definitely D!');

SELECT SA.[AnswerID]
      ,SA.[StudentID]
      ,SA.[QuestionID]
      ,T.c.value('.', 'CHAR')
      ,QA.[Text]
FROM @StudentAnswers SA
CROSS APPLY 
(
    SELECT CAST('<i>' + REPLACE([Answers], ',', '</i><i>') + '</i>' AS XML) Answers
) DS
CROSS APPLY DS.Answers.nodes('i') T(c)
INNER JOIN @QuestionAnswers QA
    ON SA.[QuestionID] = QA.[QuestionID]
    AND T.c.value('.', 'CHAR') = QA.[Answer];

Try this 尝试这个

select answerid,studentid,a.QuestionID,group_concat(b.text) from student_answers a left join QUESTION_ANSWERS b on b.questionid= a.questionid and  FIND_IN_SET(b.Answer, a.Answers)
group by a.questionid

Definitely it works. 绝对有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM