简体   繁体   English

类别偏移

[英]Categories off-set

I am about to build an SQL database and I need to get it right instead of making mistakes and fixing them down the line which is why I am here... 我将要建立一个SQL数据库,我需要正确处理它,而不是犯错误并改正这些错误,这就是我在这里的原因...

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. 因此,如果lcid设置为0,则其为主要类别,但是如果lcid中包含与cid相关的数字,则其为子类别。

Then I would use php to link them together and display them. 然后,我将使用php将它们链接在一起并显示它们。

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. 在这里查看优缺点的描述。

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

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