peixue-dev/Archive/[一次性]修复userId1消息数据-2026-01-27.sql

35 lines
1.2 KiB
SQL
Raw Permalink 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.

-- ============================================
-- 修复 userId=1 的消息数据问题
-- 创建时间: 2026-01-27
-- 注意:执行前先备份数据!
-- ============================================
-- 方案A删除异常的消息记录如果有NULL字段
-- DELETE FROM message
-- WHERE (user_id = 1 OR receiver_id = 1)
-- AND (
-- id IS NULL
-- OR user_id IS NULL
-- OR receiver_id IS NULL
-- OR content IS NULL
-- OR created_time IS NULL
-- );
-- 方案B清空用户1的所有消息如果数据太多或太乱
-- DELETE FROM message WHERE user_id = 1 OR receiver_id = 1;
-- 方案C修复特定字段的NULL值
-- UPDATE message
-- SET is_read = 0
-- WHERE (user_id = 1 OR receiver_id = 1) AND is_read IS NULL;
-- 方案D如果用户1不存在创建用户1
-- INSERT INTO user (id, phone, password, name, role, status, created_time)
-- VALUES (1, '13800138000', '$2a$10$...', '张爸爸', 'user', 1, NOW())
-- ON DUPLICATE KEY UPDATE id = id;
-- 验证修复结果
SELECT '=== 验证修复结果 ===' AS step;
SELECT COUNT(*) as total_messages FROM message WHERE user_id = 1 OR receiver_id = 1;
SELECT COUNT(*) as unread_count FROM message WHERE receiver_id = 1 AND is_read = 0;