Ordering items with matching tags by number of tags that match

前端 未结 2 1535
[愿得一人]
[愿得一人] 2021-02-04 13:10

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
2条回答
  •  渐次进展
    2021-02-04 13:44

    Something resembling:

    SELECT a.* 
    FROM articles AS a 
    INNER JOIN articles_tags AS at ON a.id=at.article_id
    INNER JOIN tags AS t ON at.tag_id = t.id
    WHERE t.title = 'funny' OR t.title = 'goofy' OR t.title = 'silly' AND a.id != 
    GROUP BY a.id
    ORDER BY COUNT(a.id) DESC
    

    With just the usual indexes, assuming articles_tags has PK of (article_id, tag_id), and an index on tags.title

提交回复
热议问题