在我的项目中,当用户观看视频时,它将被插入到用于存储已观看电影详细信息的表格中。
我正在使用以下查询来获取当前用户按最新记录顺序查看的电影。如果用户多次观看过视频,则应仅返回其中的最新记录。
我正在使用以下查询。
SELECT vm.watched_on,vm.movie_id,cp.channel_id,
v.title, v.videothumbnail, v.views, v.likes, v.length, v.videoid,
cp.title as videotitle, cv.thumbnail,cv.views as cvviews,cv.likes as cvlikes, cv.id,cp.starttime,cp.endtime,vm.videotype
FROM viewedmovies as vm
LEFT JOIN video as v
ON (vm.movie_id=v.videoid AND vm.user_id=1 AND vm.videotype=1 and v.permission!=-1)
LEFT JOIN channel_videos as cv
ON (vm.movie_id=cv.id AND vm.user_id=1 AND (vm.videotype=0 or vm.videotype=2))
LEFT JOIN channel_programmes as cp
ON (vm.movie_id=cp.programme_source AND (vm.videotype=0 or vm.videotype=2) AND cp.status!=-1)
where vm.user_id=1 GROUP BY vm.user_id, vm.movie_id, vm.videotype
ORDER BY vm.watched_on desc
LIMIT 6
视频存储在两个表中:如果 Viewedmovies 中的视频为1,它将从视频表中查询,并且 videotype 是否为0 ,它将从 channel_programmes 和 channel_videos 进行查询。此查询返回结果,但不按日期顺序返回。
http://sqlfiddle.com/#!2/0c382/1
任何人都可以帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:0)
http://sqlfiddle.com/#!2/0c382/55
SELECT vm.watched_on,vm.movie_id,cp.channel_id,
v.title, v.videothumbnail, v.views, v.likes, v.length, v.videoid,
cp.title as videotitle, cv.thumbnail,cv.views as cvviews,cv.likes as cvlikes, cv.id,cp.starttime,cp.endtime,vm.videotype
FROM
(
SELECT x.*
FROM
(
SELECT user_id, movie_id, watched_on, videotype
FROM viewedmovies
ORDER BY watched_on DESC
) as x
GROUP BY x.user_id, x.movie_id
) as vm
LEFT JOIN video as v
ON (vm.movie_id=v.videoid AND vm.user_id=1 AND vm.videotype=1 and v.permission!=-1)
LEFT JOIN channel_videos as cv
ON (vm.movie_id=cv.id AND vm.user_id=1 AND (vm.videotype=0 or vm.videotype=2))
LEFT JOIN channel_programmes as cp
ON (vm.movie_id=cp.programme_source AND (vm.videotype=0 or vm.videotype=2) AND cp.status!=-1)
WHERE vm.user_id=1
ORDER BY vm.watched_on desc