How to design a MySql Table for a Tag Cloud?

前端 未结 3 1742
有刺的猬
有刺的猬 2021-02-03 13:13

I have articles on my site, and I would like to add tags which would describe each article, but I\'m having problems with design mysql table for tags. I have two ideas:

3条回答
  •  悲哀的现实
    2021-02-03 13:57

    First off, you'll want to use Pascal MARTIN's suggestion about the table design.

    As for finding similar articles, here's something to get you started. Given that @article_id is the article you want to find matches for, and @tag1, @tag2, @tag3 are the tags for that article:

    SELECT article_id, count(*)
    FROM tags_articles
    WHERE article_id <> @article_id
    AND tag_id IN (@tag1, @tag2, @tag3)
    GROUP BY article_id
    ORDER BY count(*) DESC
    LIMIT 3
    

提交回复
热议问题