Search code examples
phpmysqlvideorelated-content

Get videos related to video id X


I'm trying to select 6 related videos related to video id X. I've looked through several questions here on SO but none matches my system with genres and such. What I've tried to do was to make an UNION SELECT but that kept listing the same video over and over again, so I gave up.

I have three databases which are off essence right now.

videos - Table containing all videos available. vID vName vCover

genres - A table containing available genres (action, drama, thriller etc.) gID gName

genre_connections - This table contains connections between a genre and a video (thriller -> inception (but with their IDs)) gcID gcVideoID gcGenreID

I want to select 6 videos which are related to the supplied ID. Relations are based on similar genres. So if I'm reviewing a horror/thriller movie the most related video would be a horror/thriller movie and the second most related video would be a horror or thriller movie.

Basically the relations should be based on movies with the same genres ordered by total genres in common, descending.

If it's possible to include would not much hassle a relation between names would also be great. So if I'm reviewing Iron Man the top related videos would be Iron Man 2 and Iron Man 3 and then the genre-related movies would follow.


Solution

  • Not tested (no real test data) but the first part of your question could probably be done with a single query like this:-

    SELECT d.vID, d.vName, GROUP_CONCAT(e.gName) AS SharedGenre, COUNT(*) AS SharedGenreCount
    FROM videos a
    INNER JOIN genre_connections b
    ON a.vID = b.gcVideoID
    INNER JOIN genre_connections c
    ON b.gcGenreID = c.gcGenreID
    AND b.gcVideoID != c.gcVideoID
    INNER JOIN videos d
    ON c.gcVideoID = d.vID
    INNER JOIN genres e
    ON c.gcGenreID = e.gID
    WHERE a.vName = 'Iron Man'
    GROUP BY d.vID, d.vName
    ORDER BY SharedGenreCount DESC
    LIMIT 6
    

    For the 2nd part, to match up on similar names, I am not sure how usable it will be. For example, if you search for Dexter do you really want Dexters Laboratory returned? Similarly searching for Aliens on a simple match on name wouldn't find Alien.

    I would suggest you possibly need another table to link up a series of films.