在我的项目中,我有两种类型的视频:免费视频和精选视频。我正在 channel_videos 的表格中保存免费视频的详细信息,如下所示:
id videotitle video thumbnail added_on
我正在视频表中保存精选视频的详细信息。
videoid title videolink videothumbnail added_on
当用户观看视频时,观看的视频详细信息以及查看的用户详细信息将存储在另一个表 Viewedmovies 中。
id user_id movie_id videotype watched_on
在此表中,如果字段 videotype 的值为0(免费视频),则查询表 channel_videos ,如果 videotype 是1(精选视频)然后查询表视频。
select viewedmovies.id,viewedmovies.watched_on,video.title,video.videothumbnail,video.views,
video.likes,video.length,video.videoid from viewedmovies INNER JOIN video ON viewedmovies.movie_id=video.videoid
and viewedmovies.user_id=1 group by viewedmovies.user_id,viewedmovies.movie_id,viewedmovies.videotype order by watched_on desc LIMIT 6
以上是我使用过的查询。但这仅从视频表中获取结果。
如何在单个查询中执行此操作?另外,我希望将结果分组到 seenmovies 表的movie_id,videotype和user_id字段中。
提前致谢。
答案 0 :(得分:1)
我没有测试过,但是这样的事情应该有效:
SELECT viewedmovies.id, viewedmovies.watched_on
, video.title, video.videothumbnail, video.views, video.likes, video.length, video.videoid
, channel_videos.videotitle, channel_videos.thumbnail, channel_videos.id
FROM viewedmovies
LEFT JOIN video
ON (viewedmovies.movie_id=video.videoid AND viewedmovies.user_id=1 AND viewedmovies.videotype=1)
LEFT JOIN channel_videos
ON (viewedmovies.movie_id=channel_videos.id AND viewedmovies.user_id=1 AND viewedmovies.videotype=0)
GROUP BY viewedmovies.user_id, viewedmovies.movie_id, viewedmovies.videotype
ORDER BY watched_on desc
LIMIT 6