我有三种产品类型。以后会更的。 我也有产品类别。类别不能有多个产品类型。那么,我应该如何保留类别。
1.id |父 ID |姓名
我会将产品类型设置为根类别。
例如:
Electronic > computer > laptop
Electronic 的父 ID 将为 0,下一个类别将因此链接。
id |父 ID |产品类型 |姓名
我将从 product_type 获取电子产品,计算机的 parent_id 将为 0。
哪种方式更好?
最佳答案
对于类别:
ID | Name | Parent_ID
(父ID为类别树)
对于产品:
ID | Category_ID | Name | Description | Other
假设类别树是这样的
Computers [ID: 1 | Parent: 0]
|-Laptops [ID: 2 | Parent: 1]
|-Desktop [ID: 3 | Parent: 1]
|-Printers [ID: 4 | Parent: 1]
| |-Brand #1 [ID: 5 | Parent: 4]
| |-Brand #2 [ID: 6 | Parent: 4]
|-Other stuff [ID: 7 | Parent: 1]
然后,如果你想显示来自的所有产品,例如Printers
, 只需递归从 Printers
获取所有项目类别及其子类别。
长话短说
第一种方式更好
关于database-design - 在数据库中存储类别和产品类型的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741398/