I need to find distinct videos that are not viewed or uploaded by user logged in and are ordered by the number of instances found in a MySQL search
And it finds the needed videos as it should but it finds videos that contain more than 1 of the searched tags the number of tags searched for times and all with the same score
So if it searches for tag1 and tag2 and 1 video contains tag1 - and another video contains tag1 and tag2
The result is:
Video1 - single score
Video2 - single score
Video2 - single score
All 3 with the same score
And what I need is for it to find only distinct videos but which score reflects how many tags of the searched ones it contains
The needed result: (in correct order)
Video2 - double score (contains 2 tags)
Video1 - single score (contains 1 tag)
If you want to test you can click the link below and change
the 2 instances of userID = ... (1,2,3 to choose from)
and
the 2 instances of tags MATCH (vtn.tag) AGAINST ('...' IN BOOLEAN MODE) (politics,news,social,other to chose from)
(just remember to make the 2 instances of userID the same and the 2 instances of tags the same)
To that end I have the following SELECT statement
SELECT v.id AS vID , v.date AS vDate , v.seriestitle AS vSeriestitle , v.seriesnumber AS vSeriesnumber , v.title AS vTitle , v.randomString AS vRandomString , u.username AS uUsername , u.randomString AS uRandomString , ( SELECT COUNT(*) FROM videoview AS vv WHERE vv.videoID = v.id ) AS videoviews , MATCH ( vtn.tag ) AGAINST ( '$tags' IN BOOLEAN MODE ) AS score FROM video AS v LEFT JOIN userlogininfo AS u ON v.userID = u.id LEFT JOIN videotag AS vt ON vt.videoID = v.id LEFT JOIN videotagname AS vtn ON vtn.id = vt.tagID WHERE MATCH ( vtn.tag ) AGAINST ( '$tags' IN BOOLEAN MODE ) AND v.userID != ? AND NOT EXISTS ( SELECT NULL FROM videoview AS vvn WHERE vvn.userID = ? AND vvn.videoID = v.id ) ORDER BY score DESC LIMIT ?
Or more readable:
SELECT
v.id AS vID ,
v.date AS vDate ,
v.seriestitle AS vSeriestitle ,
v.seriesnumber AS vSeriesnumber ,
v.title AS vTitle ,
v.randomString AS vRandomString ,
u.username AS uUsername ,
u.randomString AS uRandomString ,
( SELECT
COUNT(*)
FROM videoview AS vv
WHERE vv.videoID = v.id ) AS videoviews ,
MATCH ( vtn.tag ) AGAINST ( '$tags' IN BOOLEAN MODE ) AS score
FROM video AS v
LEFT JOIN userlogininfo AS u
ON v.userID = u.id
LEFT JOIN videotag AS vt
ON vt.videoID = v.id
LEFT JOIN videotagname AS vtn
ON vtn.id = vt.tagID
WHERE
MATCH ( vtn.tag ) AGAINST ( '$tags' IN BOOLEAN MODE )
AND v.userID != ?
AND NOT EXISTS
( SELECT NULL FROM videoview AS vvn WHERE vvn.userID = ?
AND vvn.videoID = v.id )
ORDER BY score DESC
LIMIT ?