一篇关于多个类别的文章 [英] one article on multiple category

查看:74
本文介绍了一篇关于多个类别的文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文章添加到多个类别 例如,我有文章名称"test article",需要将其添加到类别1和类别2中 需要知道如何在表之间建立关系

I need to add article to multiple category for example I have article name 'test article' need to add it to category 1 and category2 need to know how to make relationship between to tables for this

注意:过去,我在文章数据库表名称"category"中添加一行,并添加类别ID(例如1,2,3),但这在搜索和列表中造成问题,请为此提供帮助

note : in the past I make row in article database table name 'category' and add category id like 1,2,3 but this make problem in search and list please help me about that

推荐答案

您需要三个表:

  • articles
  • 的一个表
  • categories
  • 的一个表
  • 一个将文章链接到类别的表,我们可以称其为categories_per_article.该表通常称为 Junction表或Association表.
  • One table for articles
  • One table for categories
  • One table linking articles to categories, we might call it categories_per_article. This table often is called a Junction table or a Association table.

示例数据:

ARTICLES
ID | Name | Description
1  | Fuzz | A fuzzy three
2  | Bizz | A five that means bizznezz

CATEGORIES
ID  |  Name 
1   |  Prime numbers
2   |  Multiples of five
3   |  Smaller than four

CATEGORIES_PER_ARTICLE
ID | ARTICLE_ID | CATEGORY_ID
1  | 1          | 1
2  | 1          | 2
3  | 1          | 3
4  | 2          | 1

您将看到第1条(模糊)具有三个类别,第2条(Bizz)仅具有类别1.我们将其称为

You will see that article 1 (Fuzz) has three categories, and article 2 (Bizz) only has category 1. We call this a many-to-many relationship (or n-to-n, for example in ER or UML), because article 1 has multiple categories, and category 1 is used by multiple articles.

您可以使用此架构进行所有可能的查询-随时在注释中询问具体示例(例如How do I get all articles which have categories 1 and 3 but not 2).

You can do all possible queries using this schema - feel free to ask in comments for specific examples (like How do I get all articles which have categories 1 and 3 but not 2).

这篇关于一篇关于多个类别的文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆