MySQL / Moodle - 用一个以管道分隔的文本字段

时间:2018-01-15 20:40:09

标签: mysql sql moodle

我正在尝试使用SQL从位于MySQL数据库中的Moodle服务器中提取调查问题和答案。 (Moodle版本3.1.7(版本:20170710))

商店提问的mdl_feedback_item表,而答案是通过mdl_feedback_value表的连接获得的。

这是我目前的sql脚本:

select fi.id, fi.name as fb_question, fi.presentation as answer_options, fv.value as answer
from mdl_feedback fb
left join mdl_feedback_item fi on fi.feedback=fb.id
left join mdl_feedback_value fv on fv.item=fi.id

结果是:

id / fb_question / answer_options / answer
68 / Do you ... / Agree|Neutral|Disagree / 3
69 / Are you... / Yes|No|Maybe / 1
70 / Which day... / Monday|Tuesday / 2
(...)

我的目标是检查'answer'值,并将其替换为相应的answer_option: id / fb_question / answer_options / answer
68 / Do you ... / Agree|Neutral|Disagree / Disagree
69 / Are you... / Yes|No|Maybe / Yes
70 / Which day... / Monday|Tuesday / Tuesday

每个问题没有固定的最小/最大替代数量,因此我认为我需要检查答案'值',然后将其与管道分隔的answer_options列中的相应位置匹配。

有没有人有这种提取的经验?

谢谢!

1 个答案:

答案 0 :(得分:0)

我想我找到了答案。

所以,通过使用LENGTH,我基本上可以找出我对特定课程有多少答案选择:

Max((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1) as max_num_options,

我们说结果是5.

然后我在CASE语句中使用SUBSTRING_INDEX,这将为我提供5个替代中的每一个的值。

CASE

WHEN fv.value = 1 THEN (IF(SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', ((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1) ), '|', -1 ) = SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', (((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1)-1) ), '|', -1 ),"",SUBSTRING_INDEX( SUBSTRING_INDEX(fi.presentation, '|', 1 ), '|', -1 )))

WHEN fv.value = 2 THEN (IF(SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', ((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1) ), '|', -1 ) = SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', (((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1)-1) ), '|', -1 ),"",SUBSTRING_INDEX( SUBSTRING_INDEX(fi.presentation, '|', 2 ), '|', -1 )))

WHEN fv.value = 3 THEN (IF(SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', ((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1) ), '|', -1 ) = SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', (((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1)-1) ), '|', -1 ),"",SUBSTRING_INDEX( SUBSTRING_INDEX(fi.presentation, '|', 3 ), '|', -1 )))

WHEN fv.value = 4 THEN (IF(SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', ((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1) ), '|', -1 ) = SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', (((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1)-1) ), '|', -1 ),"",SUBSTRING_INDEX( SUBSTRING_INDEX(fi.presentation, '|', 4 ), '|', -1 )))

WHEN fv.value = 5 THEN (IF(SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', ((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1) ), '|', -1 ) = SUBSTRING_INDEX(SUBSTRING_INDEX(fi.presentation, '|', (((LENGTH(fi.presentation) - LENGTH(REPLACE(fi.presentation, '|', '')))+1)-1) ), '|', -1 ),"",SUBSTRING_INDEX( SUBSTRING_INDEX(fi.presentation, '|', 5 ), '|', -1 )))

ELSE 'No answer given / text field'
END AS fv_response_text

它有点乱,而且我总是要事先检查有多少替代品,但是它完成了工作。