This question is similar to this one but with a difference: I have eight different types of tags.
Something like this:
videos [id, title]
tags1 [id, tag]
tags2 [id, tag]
tags3 [id, tag]
tags4 [id, tag]
tags5 [id, tag]
tags6 [id, tag]
tags7 [id, tag]
tags8 [id, tag]
video_tags1 [vid_id, tag_id]
video_tags2 [vid_id, tag_id]
video_tags3 [vid_id, tag_id]
video_tags4 [vid_id, tag_id]
video_tags5 [vid_id, tag_id]
video_tags6 [vid_id, tag_id]
video_tags7 [vid_id, tag_id]
video_tags8 [vid_id, tag_id]
Given a single video.id I want to pick out related videos based on which have the most tags in common. I’m finding it hard to find a way to do this, let alone a way that won’t bring the server to it’s knees.
If you can change your database model then these proposal may fit you.
Redefine your tables in this way :
you may also add a tag_type table (which will have 8 rows, one for each of yourtag types) for more consistency.
Then this query (it may has some syntax error, but my intention is that you get the idea) will give you the video ids and the number of matching tags with the one provided :
You may add some more logic in order to cut down the results if the
nEqualTagsis lower than certain value.(And also consider adding some index for better performance).
Hope it helps