简体   繁体   English

文章和类别-有效的方法

[英]Articles and Categories - The efficient way

I want to add categories to my website - but with more complex sorting. 我想将类别添加到我的网站-但排序更为复杂。

Lets say I have article a . 可以说我有一个文章。 now, I want a to be under main category a , but also on category c thats its parent is b . 现在,我希望a在主要类别a下 ,但在类别c上,即其父级是b

I thought of doing it like this: An article would have a main category field, and also parent_ids 我想这样做:一篇文章将有一个主类别字段,还有parent_ids

parent_ids would be every category this item is belong to. parent_ids是该项目所属的每个类别。 my problem is: lets say i have: parent_ids: |1|5|2|7| 我的问题是:可以说我有: parent_ids:| 1 | 5 | 2 | 7 | .

now i am in category id - 7 . 现在我在id- 7中

how would my select query look like? 我的选择查询看起来如何?

select * from categories where parent_ids LIKE '%|7|%'

is it efficient? 有效率吗? is there a better idea? 有更好的主意吗?

Never store multiple values in one column. 切勿将多个值存储在一列中。 All column values in a normalized database should be atomic (single-valued). 规范化数据库中的所有列值均应为原子值(单值)。

The relationship between articles and categories is many-to-many . articlescategories之间的关系是多对多的 That means you should have a separate table, such as article_category (article_id, category_id) containing one row for each category each article is in. 这意味着您应该有一个单独的表,例如article_category (article_id, category_id) ,其中每个文章article_category (article_id, category_id)的每个类别都包含一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM