如何在MySQL中设计类别和子类别? [英] How to design category and subcategories in MySQL?
问题描述
我有一个类别列表以及与每个类别关联的子类别数量. 假设类别表称为猫,那么我有热类别和冷类别 我有另一个称为subcats的表,然后有以下内容:
I have a list of categories and number of sub categories associated to each category. let say Category table is called Cat then I have hot and cold categories in it I have another table called subcats then I have the following:
猫:
ID Name
1 Hot
2 Cold
SubCats :
SubCatID CATID Name
1 1 soup
2 1 rice
3 1 pizza
4 2 salad
5 2 fruit
我应该考虑设计的性能,您如何评价我的设计?有更好的解决方案吗?
I should consider performance in my design, how do you rate my design? is there any better solution?
(类别仅是示例-我有很多类别和子类别.)
(Categories are just sample - I have heaps of categories and subcategories.)
推荐答案
您可以将所有内容放在一个表中,类别.然后有一列作为parentID.如果parentID = 0,它是一个主类别,如果它是另一个ID,则它是一个子类别?此结构将支持子子类别...不确定是否对您有帮助.
You could have everything in one table, category. Then have a column for parentID. If parentID = 0, it is a master category, if its another ID, then it is a subcategory? This structure would support sub-sub categories... not sure if that's helpful to you.
示例字段:
Table: category
categoryID
parentID
name
示例数据:
categoryID : 1
parentID : 0
name : hot
categoryID : 2
parentID : 0
name: cold
categoryID : 3
parentID : 2
name : a soup that's cold
categoryID : 4
parentID: 1
name: a soup that's hot
这篇关于如何在MySQL中设计类别和子类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!