如何为标签云设计MySql表?
我的网站上有文章,我想添加描述每篇文章的标签,但我在设计标签的 mysql 表时遇到问题。我有两个想法:
- 每篇文章都有“标签”字段,标签的格式为:“tag1,tag2,tag3”
- 创建其他名为标签的表,字段为:tag_name,article_id
因此,当我想要 ID 为 1 的文章的标签时,我会运行
SELECT ... FROM tags WHERE `article_id`=1;
但是,我还想通过比较标签来了解 3 篇最相似的文章,所以如果我有一篇文章的标签为“php,mysql,erlang”,并且 5 篇文章的标签为:“php,mysql”,“erlang, ruby”、“php erlang”、“mysql、erlang、javascript”,我会选择 1.、3. 和 4.,因为这 3 个与主文章具有大多数相同的标签。
还有另一个问题,获得 10 个“最常用标签”的最佳方法是什么?
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:
- each article would have field "tags", and tags would be in format: "tag1,tag2,tag3"
- create other table called tags with fields: tag_name, article_id
So when I want tags for article with ID 1, I would run
SELECT ... FROM tags WHERE `article_id`=1;
But, I would also like to know 3 most similar articles by comparing tags, so if I have article which has tags "php,mysql,erlang", and 5 articles with tags: "php,mysql", "erlang,ruby", "php erlang", "mysql,erlang,javascript", I would choose 1., 3. and 4., since those 3 have most same tags with main article.
Also other question, what is the best way to get 10 "most used tags" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,对于这种多对多关系,有三个表:
article
”表tag
”表tags_articles
”表,它充当连接表,仅包含:id_article
:指向文章的外键id_tag
:指向标签的外键这样,任何标签的数据都不会重复:对于每个标签,
tag
表中只有一行。并且,对于每篇文章,您可以有多个标签(即
tags_articles
表中的几行);当然,对于每个标签,您可以有几篇文章。按照这个想法,获取文章的标签列表只需进行额外的查询,例如:
获取三篇“最相似”的文章意味着:
未经测试,但一个想法可能如下所示:
基本上,您:
where
子句匹配的标签,而没有group by
子句,则会有两行那篇文章group byarticle.id
时,每篇文章将只有一行count
来找出每篇文章与第一篇文章有多少个共同标签Generally, for this kind of many-to-many relationship, there are three tables :
article
" tabletag
" tabletags_articles
" table, which acts as a join table, and contains only :id_article
: foreign key that points to an articleid_tag
: foreign key that points to a tagThis way, there is no duplication of any tag's data : for each tag, there is one, and only one, line in the
tag
table.And, for each article, you can have several tags (i.e. several lines in the
tags_articles
table) ; and, of course, for each tags, you can have several articles.Getting a list of tags for an article, with this idea, is a matter of an additionnal query, like :
Getting the three "most similar" articles would mean :
Not tested, but an idea might be something that would look like this :
Basically, you :
where
clause, without thegroup by
clause, there would be two lines for that articlegroup by article.id
, there will be only one line per articlecount
, to find out how many tags each article has in common with the initial one首先,您需要使用 Pascal MARTIN 关于桌子设计的建议。
至于查找类似的文章,这里有一些可以帮助您入门的内容。假定 @article_id 是您要查找匹配项的文章,@tag1、@tag2、@tag3 是该文章的标签:
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:
是的,但是你没有回答我的主要问题,如何获得3篇最相似的文章?
回答:
只需在合并表 (tags_articles) 中查找相同的标签 id 即可。收集它们并创建一个图案。
例如:
第 1 条有标签:1,2
第 2 条有标签:2,3,4
第 5 条有标签:6,7,2
第 7 篇文章有标签:7,1,2,3
如果您想要第 1 篇文章中最相似的 3 篇文章,则必须查找标签 1,2。您会发现第 7 条最相似,第 2 条和第 5 条有一些相似之处。
yes, but you didn't answer my main question, how to get 3 most similar articles?
Answer:
Just look for the same tag ids in the merged table (tags_articles). Gather them and create a pattern.
For example:
Article 1 has tags: 1,2
Article 2 has tags: 2,3,4
Article 5 has tags: 6,7,2
Article 7 has tags: 7,1,2,3
If you want the 3 most similar articles for article 1, you have to look for the tags 1,2. You'll find Article 7 is most similar and 2 and 5 have some similarities.