-- ========================================
-- 修复数据库中的文件路径
-- ========================================
-- 问题：文件路径中有重复的 /upload/upload/
-- 解决：替换为正确的 /upload/

USE study;

-- 1. 查看需要修复的记录
SELECT 
    id,
    name,
    file_path AS '当前路径',
    REPLACE(file_path, '/profile/upload/upload/', '/profile/upload/') AS '修复后路径'
FROM study_courseware 
WHERE file_path LIKE '/profile/upload/upload/%';

-- 2. 备份原始数据（可选但推荐）
CREATE TABLE IF NOT EXISTS study_courseware_backup_20251207 AS 
SELECT * FROM study_courseware 
WHERE file_path LIKE '/profile/upload/upload/%';

-- 3. 执行修复
UPDATE study_courseware 
SET file_path = REPLACE(file_path, '/profile/upload/upload/', '/profile/upload/')
WHERE file_path LIKE '/profile/upload/upload/%';

-- 4. 验证修复结果
SELECT 
    COUNT(*) AS '修复的记录数'
FROM study_courseware 
WHERE file_path LIKE '/profile/upload/%'
AND file_path NOT LIKE '/profile/upload/upload/%';

-- 5. 检查是否还有问题记录
SELECT 
    id,
    name,
    file_path
FROM study_courseware 
WHERE file_path LIKE '/profile/upload/upload/%';

-- 如果上面的查询返回 0 条记录，说明修复成功！

-- ========================================
-- 完成！
-- ========================================
