I'm trying to figure out how to order items with matching tags by the number of tags that match.
Let's say you have three MySQL tables:
tags(tag_id, title)
articles(article_id, some_text)
articles_tags(tag_id, article_id)
Now let's say you have four articles where:
article_id = 1
has tags "humor," "funny," and "hilarious."
article_id = 2
has tags "funny," "silly," and "goofy."
article_id = 3
has tags "funny," "silly," and "goofy."
article_id = 4
has the tag "completely serious."
You need to find all articles related to article_id = 2
by at least one matching tag, and return the results in order of the best matches. In other words, article_id = 3
should come first, with article_id = 1
second, and article_id = 4
should not show up at all.
Is this something that's doable in SQL queries or alone, or is this better suited for something like Sphinx? If the former, what kind of query should be done, and what sort of indexes should be created for the most performant results? If the latter, please do expand.