guoyu/统一题目格式并修复.sql

240 lines
7.9 KiB
MySQL
Raw Normal View History

2025-12-06 20:11:36 +08:00
-- ====================================================
-- 统一题库和考试题目格式,确保数据一致性
-- ====================================================
-- 问题:从题库导入题目后显示没有题目
-- 原因:数据格式不一致或字段为空
-- ====================================================
-- 第1步检查考试题目
-- ====================================================
-- 查看考试和题目数量
SELECT
e.id AS ID,
e.exam_name AS ,
e.question_count AS ,
COUNT(q.id) AS ,
CASE
WHEN e.question_count = COUNT(q.id) THEN '✓ 一致'
WHEN e.question_count > COUNT(q.id) THEN '❌ 记录比实际多'
WHEN e.question_count < COUNT(q.id) THEN '❌ 实际比记录多'
END AS
FROM study_exam e
LEFT JOIN study_question q ON e.id = q.exam_id
GROUP BY e.id, e.exam_name, e.question_count
ORDER BY e.create_time DESC
LIMIT 10;
-- 查看题目详情
SELECT
q.id AS ID,
q.exam_id AS ID,
e.exam_name AS ,
q.question_type AS ,
LEFT(q.question_content, 30) AS ,
q.options AS JSON,
q.correct_answer AS ,
q.score AS ,
q.source_type AS ,
q.sort_order AS
FROM study_question q
LEFT JOIN study_exam e ON q.exam_id = e.id
WHERE e.exam_name LIKE '%Test%' -- 根据考试名称过滤
ORDER BY q.exam_id, q.sort_order;
-- ====================================================
-- 第2步检查数据完整性问题
-- ====================================================
-- 检查缺失必填字段的题目
SELECT
'考试题目' AS ,
q.id,
q.exam_id,
CASE
WHEN q.question_type IS NULL OR q.question_type = '' THEN '❌ 题型为空'
WHEN q.question_content IS NULL OR q.question_content = '' THEN '❌ 题干为空'
WHEN q.correct_answer IS NULL OR q.correct_answer = '' THEN '❌ 答案为空'
WHEN q.score IS NULL OR q.score = 0 THEN '❌ 分值为空或0'
WHEN q.options IS NULL OR q.options = '' THEN '⚠️ 选项为空'
ELSE '✓ 完整'
END AS
FROM study_question q
WHERE q.question_type IS NULL OR q.question_type = ''
OR q.question_content IS NULL OR q.question_content = ''
OR q.correct_answer IS NULL OR q.correct_answer = ''
OR q.score IS NULL OR q.score = 0
ORDER BY q.exam_id, q.id;
-- 检查题库题目
SELECT
'题库题目' AS ,
i.id,
i.bank_id,
CASE
WHEN i.question_type IS NULL OR i.question_type = '' THEN '❌ 题型为空'
WHEN i.question_content IS NULL OR i.question_content = '' THEN '❌ 题干为空'
WHEN i.correct_answer IS NULL OR i.correct_answer = '' THEN '❌ 答案为空'
WHEN i.score IS NULL OR i.score = 0 THEN '❌ 分值为空或0'
WHEN i.options IS NULL OR i.options = '' THEN '⚠️ 选项为空'
ELSE '✓ 完整'
END AS
FROM study_question_bank_item i
WHERE i.question_type IS NULL OR i.question_type = ''
OR i.question_content IS NULL OR i.question_content = ''
OR i.correct_answer IS NULL OR i.correct_answer = ''
OR i.score IS NULL OR i.score = 0
ORDER BY i.bank_id, i.id;
-- ====================================================
-- 第3步检查options字段格式
-- ====================================================
-- 查看选项格式应该是JSON数组格式
SELECT
'考试题目' AS ,
id,
question_type,
LEFT(question_content, 30) AS ,
options,
CASE
WHEN options IS NULL THEN '❌ NULL'
WHEN options = '' THEN '❌ 空字符串'
WHEN options LIKE '[%]' THEN '✓ JSON数组'
WHEN options LIKE '{%}' THEN '⚠️ JSON对象'
ELSE '❌ 其他格式'
END AS
FROM study_question
WHERE question_type IN ('single', 'multiple')
AND (options IS NULL OR options = '' OR NOT (options LIKE '[%]'))
LIMIT 20;
-- ====================================================
-- 第4步修复空数据
-- ====================================================
-- 为题型为空的题目设置默认值
-- UPDATE study_question
-- SET question_type = 'single'
-- WHERE question_type IS NULL OR question_type = '';
-- 为分值为0或NULL的题目设置默认分值
UPDATE study_question
SET score = 5
WHERE score IS NULL OR score = 0;
UPDATE study_question_bank_item
SET score = 5
WHERE score IS NULL OR score = 0;
-- 为排序为空的题目设置排序
SET @row_number = 0;
UPDATE study_question q
JOIN (
SELECT id, exam_id, (@row_number:=IF(@exam_id=exam_id, @row_number+1, 1)) AS new_order, (@exam_id:=exam_id)
FROM study_question
ORDER BY exam_id, IFNULL(sort_order, 999), id
) AS ranked ON q.id = ranked.id
SET q.sort_order = ranked.new_order
WHERE q.sort_order IS NULL;
-- ====================================================
-- 第5步同步考试题量和总分
-- ====================================================
-- 重新计算每个考试的题量和总分
UPDATE study_exam e
LEFT JOIN (
SELECT
exam_id,
COUNT(*) AS actual_count,
SUM(IFNULL(score, 0)) AS actual_score
FROM study_question
GROUP BY exam_id
) AS stats ON e.id = stats.exam_id
SET
e.question_count = IFNULL(stats.actual_count, 0),
e.total_score = IFNULL(stats.actual_score, 0);
-- ====================================================
-- 第6步验证修复结果
-- ====================================================
-- 查看修复后的考试信息
SELECT
e.id,
e.exam_name,
e.question_count AS ,
e.total_score AS ,
COUNT(q.id) AS ,
SUM(IFNULL(q.score, 0)) AS ,
CASE
WHEN e.question_count = COUNT(q.id) AND e.total_score = SUM(IFNULL(q.score, 0)) THEN '✓ 正确'
ELSE '❌ 不一致'
END AS
FROM study_exam e
LEFT JOIN study_question q ON e.id = q.exam_id
GROUP BY e.id
ORDER BY e.create_time DESC
LIMIT 10;
-- 查看题目完整性
SELECT
COUNT(*) AS ,
SUM(CASE WHEN question_type IS NOT NULL AND question_type != '' THEN 1 ELSE 0 END) AS ,
SUM(CASE WHEN question_content IS NOT NULL AND question_content != '' THEN 1 ELSE 0 END) AS ,
SUM(CASE WHEN correct_answer IS NOT NULL AND correct_answer != '' THEN 1 ELSE 0 END) AS ,
SUM(CASE WHEN score IS NOT NULL AND score > 0 THEN 1 ELSE 0 END) AS ,
SUM(CASE WHEN sort_order IS NOT NULL THEN 1 ELSE 0 END) AS
FROM study_question;
-- ====================================================
-- 第7步标准化数据格式
-- ====================================================
-- 确保多选题答案格式为 "A,B,C" (逗号分隔,无空格)
UPDATE study_question
SET correct_answer = REPLACE(REPLACE(correct_answer, ', ', ','), ' ,', ',')
WHERE question_type = 'multiple';
UPDATE study_question_bank_item
SET correct_answer = REPLACE(REPLACE(correct_answer, ', ', ','), ' ,', ',')
WHERE question_type = 'multiple';
-- 确保判断题答案格式为 "正确" 或 "错误"
UPDATE study_question
SET correct_answer = CASE
WHEN LOWER(correct_answer) IN ('true', 't', '1', 'yes', '') THEN '正确'
WHEN LOWER(correct_answer) IN ('false', 'f', '0', 'no', '') THEN '错误'
ELSE correct_answer
END
WHERE question_type = 'judge';
UPDATE study_question_bank_item
SET correct_answer = CASE
WHEN LOWER(correct_answer) IN ('true', 't', '1', 'yes', '') THEN '正确'
WHEN LOWER(correct_answer) IN ('false', 'f', '0', 'no', '') THEN '错误'
ELSE correct_answer
END
WHERE question_type = 'judge';
-- ====================================================
-- 完成!执行说明
-- ====================================================
-- 1. 依次执行上面的检查SQL找出问题
-- 2. 执行修复SQLUPDATE语句
-- 3. 执行验证SQL确认修复成功
-- 4. 重新刷新后台页面
-- 5. 重新创建考试并导入题目测试
-- 注意执行UPDATE前建议备份数据库