在数据库中存储标签的最佳方式? [英] Best way to store tags in a database?
本文介绍了在数据库中存储标签的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含两个表格的数据库:
I have a database that contains two tables:
- 条目
- >
- entries
- tags
entries表包含每个都有一个或多个标签的帖子。问题是,每个帖子可以有任意数量的标签。换句话说,我不能有一个'tag1','tag2'等列,并做一个LEFT JOIN。
The entries table contains posts that each have one or more tags. The problem is, each post can have any number of tags. In other words, I can't have a 'tag1', 'tag2', etc. column and do a LEFT JOIN.
我应该如何设置条目每个帖子可以有任意数量的标签?
How should I set up entries so that each post can have any number of tags?
推荐答案
您需要一个映射表。
创建一个名为 entries_tags
的表,其中包含两个列: entry_id
和 tag_id
Create a table called entries_tags
that contains two columns: entry_id
and tag_id
, with a multi-key on both entries.
这是一个多对多关系。
这篇关于在数据库中存储标签的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文