首先,我不会在产品上设置类别 ID 和子类别 ID。它只属于一个子类别。如果需要,可以通过子类找到父类。
products (id, subcategory_id, name)
categories (id, name)
subcategories (id, category_id, name)
对于所有产品可能不同的属性,您可以这样做:
attribute_types (id, name)
attributes (id, attribute_type_id, value)
products_attributes (id, product_id, attribute_id)
所以在你的 attribute_types 表中你可能有:
id | name
-----------------------------
1 | T-Shirt Size
2 | Shoe Size (US)
3 | Colour
4 | Batteries Included
在您的属性表中:
id | attribute_type_id | value
------------------------------
1 | 1 | Small
2 | 1 | Medium
3 | 1 | Large
4 | 2 | 9
5 | 2 | 9.5
6 | 2 | 10
7 | 2 | 10.5
8 | 2 | 11
9 | 3 | Red
10 | 3 | Blue
11 | 3 | Green
12 | 3 | Yellow
13 | 3 | Black
14 | 4 | Yes
15 | 4 | No
然后在您的 products_attributes 表中,您只需添加该产品具有的任何属性:
id | product_id | attribute_id
------------------------------
1 | 456 | 6
2 | 456 | 13
所以对于那个产品来说,它是 10 号鞋,黑色。