guoyu/Test/sql/execute_fix_step_by_step.sql

118 lines
3.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ==========================================
-- 题目格式修复 - 逐步执行
-- 请按照注释一步一步执行,不要一次性全部执行
-- ==========================================
-- ==========================================
-- 第1步查看最近的考试找到要修复的考试ID
-- ==========================================
SELECT id, exam_name, create_time
FROM exam
ORDER BY id DESC
LIMIT 10;
-- 执行后找到你要修复的考试ID记下来
-- 例如id = 14
-- ==========================================
-- 第2步查看该考试的题目检查是否有问题
-- ==========================================
-- 将下面的 14 替换成你的考试ID
SELECT
id,
question_type,
question_content,
LEFT(options, 100) as options_preview,
correct_answer
FROM question
WHERE exam_id = 14
ORDER BY id;
-- 如果看到 options 中有 "A. "、"B. " 这样的前缀,说明需要修复
-- ==========================================
-- 第3步备份数据必须执行
-- ==========================================
CREATE TABLE question_backup_20251205 AS
SELECT * FROM question;
-- 验证备份是否成功
SELECT COUNT(*) as backup_count FROM question_backup_20251205;
-- ==========================================
-- 第4步修复题目核心步骤
-- ==========================================
-- 将下面的 14 替换成你的考试ID
UPDATE question
SET
options = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
options,
'"A. ', '"'),
'"B. ', '"'),
'"C. ', '"'),
'"D. ', '"'),
'"E. ', '"'),
'"F. ', '"'),
correct_answer = CASE
WHEN correct_answer LIKE 'A. %' THEN 'A'
WHEN correct_answer LIKE 'B. %' THEN 'B'
WHEN correct_answer LIKE 'C. %' THEN 'C'
WHEN correct_answer LIKE 'D. %' THEN 'D'
WHEN correct_answer LIKE 'E. %' THEN 'E'
WHEN correct_answer LIKE 'F. %' THEN 'F'
ELSE correct_answer
END
WHERE exam_id = 14
AND question_type IN ('single', 'multiple');
-- ==========================================
-- 第5步验证修复结果
-- ==========================================
-- 将下面的 14 替换成你的考试ID
SELECT
id,
question_content,
options,
correct_answer
FROM question
WHERE exam_id = 14
ORDER BY id;
-- 检查结果:
-- options 应该是: ["选项内容1","选项内容2"] ✅
-- correct_answer 应该是: A ✅
-- ==========================================
-- 第6步检查学生答案如果有人已经做过题
-- ==========================================
-- 将下面的 14 替换成你的考试ID
SELECT
sa.id,
sa.student_id,
sa.question_id,
sa.student_answer,
q.correct_answer
FROM student_answer sa
JOIN question q ON sa.question_id = q.id
WHERE sa.exam_id = 14
ORDER BY sa.student_id, sa.question_id;
-- 如果 student_answer 也是 "A. 选项内容" 格式,执行下面的修复:
-- UPDATE student_answer
-- SET student_answer = CASE
-- WHEN student_answer LIKE 'A. %' THEN 'A'
-- WHEN student_answer LIKE 'B. %' THEN 'B'
-- WHEN student_answer LIKE 'C. %' THEN 'C'
-- WHEN student_answer LIKE 'D. %' THEN 'D'
-- ELSE student_answer
-- END
-- WHERE exam_id = 14;
-- ==========================================
-- 完成!
-- ==========================================
-- 如果一切正常,修复完成
-- 如果出现问题,可以从备份恢复:
-- DELETE FROM question WHERE exam_id = 14;
-- INSERT INTO question SELECT * FROM question_backup_20251205 WHERE exam_id = 14;