I’m looking for some general advice on how to go about finding items ‘like’ the current one.
In my current example I have three tables like so (omitting unrelated data):
games
-game_id
genres
-genre_id
genres_data
-game_id
-genre_id
How can I go about finding games that have genres in common with the current one, from the ones that have all the same genres, descending to ones that only have one in common with it (limited of course to a few rows) with a given row from games?
What’s the preferred method of finding like items?
Try this:
The subquery grabs a list of all
genre_ids associated with your game, and the main query uses that to searchgenres_datafor any record that matches one of them. In other words, it searches for any game which is associated with any genre your “search game” is associated with.Because a game can have multiple genres, this query would return the same
game_idmultiple times, and if you also reported thegenre_idon these records they would each show a differentgenre_id. What we do to find the ones with the most in common is to group the results by eachgame_id, and we add theCOUNT(genre_id)in the mainSELECTto show how many differentgenre_ids there were for eachgame_idreturned in that query.From there, it’s a simple matter of ordering that count of common genres in descending order, so that the games with the most genres in common will be listed first.
I also added a second criterion to the main query to exclude the game you’re searching on from the results, otherwise that game would always have the most matches, for obvious reasons.
Hope that helps.