开发者

Categories off-set

I am about to build an SQL database and I need to get it right instead of making mistak开发者_开发问答es and fixing them down the line which is why I am here...

I will have main categories and sub categories to them, I need to know weather it'd be best to store the sub categories in the same tables as the main but just offset them for example:

Protein | Fat Loss | (two main categories)
Whey    | example  | (these would be linked)

I was thinking of designing the table like this:

cid, name, lcid (linked number)

So if lcid is set to 0 then its a main category but if its got a number relating to a cid in it then its a sub-category.

Then I would use php to link them together and display them.


What you're talking about is called an Adjacency List. It is useful for creating a hierarchy that changes often but doesn't have to provide much information. If, however, your hierarchy changes less often, but has to answer questions like "how many siblings/ancestors does this node have", a Nested Set model will do you better. Have a look here for a description of pros and cons.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜