如何管理多标记/多分类文章/帖子的数据库和查询

时间:2014-08-02 07:04:23

标签: php mysql

我有一个文章网站,每篇文章都有一些标签。我的数据库结构应该如何?

这是我的文章表(文章):

article_id (key)  |  article_content  |  article_title  |  article_lang_id

这是我的代码(tag_list)列表:

tag_list_id (key)  |  tag_list_name  |  tag_list_description

这是我的标签表(tag_link)

  tag_id (key)   |   article_id (f-key)  |   tag_list_id (f-key)

我用php回复文章内容,我怎么回应(我的意思是显示标签下面的内容)标签低于内容?

这是我的问题:

1-如何在内容附近显示每个内容标记?

(每个内容可以少于5个标签)

2 - 我可以运行哪些MYSQL查询来选择包含内容的标签? (有没有专业的MySQL命令来选择带有tag_link标签的内容,然后从tag_list中选择标签的名称,然后在文章附近显示它,我可以用连接,拥有,和其他几个MySQL命令吗?如何?)

3 - 我的数据库结构是否标记为真?

(我可以用php变量和......来做,但我需要通过MySQL来做)

2 个答案:

答案 0 :(得分:0)

这会将与逗号分隔值相关的所有标记列入文章: 如果需要,您可以按特定的article_id过滤。

SELECT 
article_contents article,group_concat(tag_list_name) tags
FROM
article a
INNER JOIN tag_link m article_id=a.id
INNER JOIN tag_list t tag_list_id=t.id
WHERE article_id="??"

您的数据库结构对我来说似乎是对的。 “article”和“tags”表之间有很多对很多的关系,你需要另外一个表进行映射。你拥有它。

答案 1 :(得分:0)

如果你有:

a table for Articles(tablename=> articles)
a table for Tags(tablename=> tags)
a table for Tags which are assigned to articles (tablename=> tags_articles)
// note the naming of the third table which shows its a relation between the first table and // the second

当用户点击文章链接进行查看时,通常会发生以下情况:

文章内容(标题,描述,图像等)是从DB中提取并回显的。

根据文章的ID,提取tags_article中标签的相关ID,并从标签表中提取标签的每个信息(通常只有id和title)。

您可以从SQL中的一个名为JOINT的查询中获取它:

您选择文章的字段,然后将其与tags_articles表关联,最终通过其ID检索与该产品关联的标记列表。从字面上看,这样的事情发生了:

  

从文章表中选择一个ID为$ id的文章。

     

然后,请从tags_articles为我选择tag_id和article_id   article_id为$ id的表格;

     

最后,我希望您选择所有标签信息(可能只有id   和标题)来自标签表,其id等于tags_articles.tag_id

SQL查询:

SELECT articles.id, articles.title, articles.description, articles.image, tags.id, tags.title FROM articles INNER JOIN tags_articles ON articles.id=tags_articles.article_id INNER JOIN tags ON tags.id=tags_articles.tag_id WHERE articles.id=$id

修改

如果您的文章ID为1且标题为"计算机软件历史记录",则它有两个标记为" history"和"软件"然后恰好是:

在文章表中:

   id : 1
   title: Computer Software History
   image: path/to/image
   description : A long long description

在标签表中有许多标签:

   id: 1, title: Computer
   id: 2, title: History
   id: 3, title: Youtube
   id: 4, title: Web
   id: 5, title: Software
   id: 6, title: Hardware
   // and hundredth of other tags

并在tags_articles表中再次为许多文章分配了许多标签......但我们专注于与您的文章相关的行ID为1:

   // many rows before
   article_id : 1, tag_id : 2 // look in the tags_table for this id
   article_id : 1, tag_id : 5 // look in the tags_table for this id
   // many rows after

通过上面的查询,您可以取出这些表的关系并提取数据并使用它。