如何在MySQL中设计类别和子类别?

编程入门 行业动态 更新时间:2024-10-23 18:25:46
本文介绍了如何在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中设计类别和子类别?

本文发布于:2023-10-30 22:40:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类别   如何在   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!