zhibo/fix_view_history_for_user.sql

83 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显示为 24187196但这可能是显示ID实际数据库ID可能不同
-- 先查询用户表确认
-- 查看所有用户
SELECT id, uid, nickname, phone FROM eb_user ORDER BY id DESC LIMIT 20;
-- 2. 检查 eb_view_history 表是否存在
SHOW TABLES LIKE 'eb_view_history';
-- 3. 检查表结构
DESC eb_view_history;
-- 4. 查看现有的观看历史数据
SELECT * FROM eb_view_history ORDER BY id DESC LIMIT 20;
-- 5. 查看直播间列表(用于添加测试数据)
SELECT id, title, streamer_id, is_live FROM eb_live_room LIMIT 10;
-- =====================================================
-- 为用户添加测试观看历史数据
-- 请根据实际用户ID修改 @USER_ID 的值
-- =====================================================
-- 设置用户ID请根据实际情况修改
-- 如果用户昵称是"道玄"先查询其真实ID
SELECT id, uid, nickname FROM eb_user WHERE nickname LIKE '%道玄%' OR uid = 24187196;
-- 假设用户ID是121道玄添加观看历史
SET @USER_ID = 121;
-- 先删除旧数据(可选)
-- DELETE FROM eb_view_history WHERE user_id = @USER_ID;
-- 插入观看历史测试数据
INSERT INTO eb_view_history (user_id, target_type, target_id, target_title, view_duration, create_time, update_time) VALUES
(@USER_ID, 'room', '1', '欢乐游戏直播', 1800, NOW() - INTERVAL 1 HOUR, NOW() - INTERVAL 1 HOUR),
(@USER_ID, 'room', '2', '音乐分享会', 2400, NOW() - INTERVAL 2 HOUR, NOW() - INTERVAL 2 HOUR),
(@USER_ID, 'room', '3', '户外探险', 1200, NOW() - INTERVAL 3 HOUR, NOW() - INTERVAL 3 HOUR),
(@USER_ID, 'room', '8', '火影忍者', 3600, NOW() - INTERVAL 30 MINUTE, NOW() - INTERVAL 30 MINUTE)
ON DUPLICATE KEY UPDATE
target_title = VALUES(target_title),
view_duration = view_duration + VALUES(view_duration),
update_time = NOW();
-- 验证插入结果
SELECT * FROM eb_view_history WHERE user_id = @USER_ID ORDER BY update_time DESC;
-- =====================================================
-- 同时检查点赞记录表
-- =====================================================
-- 检查直播间点赞表
SELECT * FROM eb_live_room_like WHERE user_id = @USER_ID;
-- 检查作品点赞表(如果存在)
SELECT * FROM eb_work_like WHERE user_id = @USER_ID;
-- 检查关注记录
SELECT * FROM eb_follow_record WHERE follower_id = @USER_ID;
-- =====================================================
-- 如果表不存在,创建它
-- =====================================================
CREATE TABLE IF NOT EXISTS `eb_view_history` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` int(11) NOT NULL COMMENT '用户ID',
`target_type` varchar(20) NOT NULL COMMENT '目标类型room-直播间, work-作品, profile-用户主页',
`target_id` varchar(50) NOT NULL COMMENT '目标ID',
`target_title` varchar(255) DEFAULT NULL COMMENT '目标标题',
`view_duration` int(11) DEFAULT 0 COMMENT '观看时长(秒)',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '查看时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_target` (`target_type`, `target_id`),
KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='查看历史记录表';