我有以下表格:
tag
id | name
1 | funny
2 | stack
3 | music
video
id | title | thumbnail | video
1 | first video | thumb.png | video1.mp4
2 | second video | thumbnail.png | anothervideo.mp4
3 | third video | woooow.png | plssomeonehelpme.mp4
video_tag
id | id_video | id_tag
1 | 1 | 1
2 | 1 | 2
3 | 2 | 2
4 | 3 | 3
video_view
id | date | id_video
1 | 2017-10-27 23:28:34 | 2
2 | 2017-10-27 23:38:04 | 2
3 | 2017-10-27 00:38:04 | 1
4 | 2017-10-28 10:38:04 | 1
5 | 2017-10-28 12:29:47 | 1
6 | 2017-10-30 15:39:33 | 3
7 | 2017-10-30 17:27:39 | 3
8 | 2017-10-30 00:22:54 | 2
我希望获得相关视频(具有更多相互标签,并按相互标签的数量排序),并获取视频的数量和按视图排序(如果具有相同的相互标签号,按次数排序。)
所以,我尝试过(假设我正在观看的视频id
是1
):
SELECT v.title, v.thumbnail, v.video, COUNT(v_t2.id) AS mutualTags
FROM video_tag v_t
INNER JOIN video_tag v_t2 ON v_t.id_tag = v_t2.id_tag
INNER JOIN video v ON v_t2.id_video = v.id
WHERE v_t.id_video = 1 AND v_t2.id_video != 1
GROUP BY v_t2.id_tag
ORDER BY mutualTags DESC
上面的代码返回错误的相互标记,我试图获取视图:
SELECT v.title, v.thumbnail, v.video, COUNT(v_t2.id) AS mutualTags, COUNT(v_v.id) AS views
FROM video_tag v_t
INNER JOIN video_tag v_t2 ON v_t.id_tag = v_t2.id_tag
INNER JOIN video v ON v_t2.id_video = v.id
INNER JOIN video_view v_v ON v_v.id_video = v_t2.id_video
WHERE v_t.id_video = 1 AND v_t2.id_video != 1
GROUP BY v_t2.id_tag, v_v.id_video
ORDER BY mutualTags DESC, views DESC
但是我得到了错误的相互标记和观点编号......那么,我该怎么做呢?
提前致谢。