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.
I thought of doing it like this: An article would have a main category field, and also parent_ids
parent_ids would be every category this item is belong to. my problem is: lets say i have: parent_ids: |1|5|2|7|.
now i am in category id - 7.
how would my select query look 开发者_JS百科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. 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.
精彩评论