我正在eCommerce网站上做一个项目,但我想不出如何为它设计数据库模式。在这个网站上,将有来自几个类别的产品,每个类别都有一套独特的过滤器。
例如:“膝上型电脑”类别有“RAM”、“硬盘”、“图形卡”等过滤器,而在“衬衫”类别中,过滤器是“Fabric Type”等等。
我考虑了两种不同的模式,但我不确定要使用哪种模式。
设计#1
categories
---------
id
category
filters (Here filters will be stored as hard-coded JSON)
products
--------
id
category_id
name
price
specifications (Stored as JSON which will be filters using values from categories.filters)设计#2
categories
----------
id
category
table_name
products
--------
id
category_id
name
price
laptops
-------
id
product_id
ram
hard_disk
shirts
------
id
product_id
fabric
sleeve我应该选择哪种设计?还是有更好的设计?
编辑
在这里,我的问题不是多个类别,而是集成过滤器。例如,如果我在膝上型电脑类别中选中“RAM 4GB”,那么它应该显示来自笔记本电脑类别的所有产品4GB RAM。我不能为每个过滤器或类别做一个表,因为它们将超过100个。
发布于 2015-08-28 01:55:54
我将通过将整个目录作为树来处理您的问题,其中每个记录代表父类别和子类别/产品之间的关系。
产品:
+----+-----------+-----------+
| id | name | child_id |
+----+-----------+-----------+
| 1 | laptops | 2 |
| 1 | laptops | 6 |
| 2 | RAM | 3 |
| 2 | RAM | 4 |
| 2 | RAM | 5 |
| 3 | RAM - 1GB | null |
| 4 | RAM - 2GB | null |
| 5 | RAM - 4GB | null |
| 6 | hard disk | null |
| 7 | shirts | 8 |
| 8 | fabrics | null |
| 9 | desktops | 2 |
| 9 | desktops | 6 |
+----+-----------+-----------+“根”类别是那些没有父母的类别。下面是查询所有根类别的方法:
SELECT DISTINCT(name)
FROM Products
WHERE id NOT IN (SELECT DISTINCT child_id FROM Products)使用我设计的示例,这将分别返回3个id值,即1、7和9,分别用于膝上型计算机、衬衫和台式计算机。如果您想要获得笔记本电脑的所有部件,可以使用以下查询:
SELECT child_id FROM Products WHERE id = 1这将返回RAM和硬盘类别的2和6。您可以根据需要继续深入查询树。例如,如果您想获得所有RAM产品,可以执行以下查询:
SELECT id, name FROM Products WHERE id IN (3,4,5) AND child_id IS NULL注意使用IS NULL识别记录是一个产品而不是一个类别,因为它没有更多的子级。
我相信即使像亚马逊和Overstock这样的大型电子商务网站也没有超过5-10级的等级,即使是在最深层的情况下也是如此。
发布于 2015-08-28 11:24:04
我在遵循@ table的设计,而不是把所有的东西都放在一张桌子上。我把它修改成不同的类别、产品和过滤器。
categories
+----+----------+-----------+
| id | category | parent_id |
+----+----------+-----------+
| 1 | Computer | NULL |
+----+----------+-----------+
| 2 | Laptop | 1 |
+----+----------+-----------+
| 3 | Desktop | 1 |
+----+----------+-----------+
| 4 | Clothing | NULL |
+----+----------+-----------+
| 5 | T-Shirt | 4 |
+----+----------+-----------+
| 6 | Shirt | 4 |
+----+----------+-----------+
products
+----+-------------+-----------------+-------+
| id | category_id | name | price |
+----+-------------+-----------------+-------+
| 1 | 2 | Acer Aspire | 600 |
+----+-------------+-----------------+-------+
| 2 | 3 | Dell All-in-One | 750 |
+----+-------------+-----------------+-------+
| 3 | 6 | Lee Marc Shirt | 50 |
+----+-------------+-----------------+-------+
| 4 | 5 | Nike T-Shirt | 100 |
+----+-------------+-----------------+-------+
filters
+----+-------------+-----------------+------------+-------------+
| id | filter | value | product_id | category_id |
+----+-------------+-----------------+------------+-------------+
| 1 | RAM | 4 GB | 1 | 2 |
+----+-------------+-----------------+------------+-------------+
| 2 | Battery | Li-ion | 1 | 2 |
+----+-------------+-----------------+------------+-------------+
| 3 | HDD | 500 GB | 1 | 2 |
+----+-------------+-----------------+------------+-------------+
| 4 | RAM | 16 GB | 2 | 3 |
+----+-------------+-----------------+------------+-------------+
| 5 | HDD | 1 TB | 2 | 3 |
+----+-------------+-----------------+------------+-------------+
| 6 | Fabric | Cotton | 3 | 6 |
+----+-------------+-----------------+------------+-------------+
| 7 | Sleeve | Full | 3 | 6 |
+----+-------------+-----------------+------------+-------------+
| 8 | Size | M | 4 | 5 |
+----+-------------+-----------------+------------+-------------+
| 9 | Color | Black | 4 | 5 |
+----+-------------+-----------------+------------+-------------+若要获取类别的所有筛选器,请执行以下操作:
SELECT DISTINCT `filters`.`filter` FROM `filters` WHERE `filters`.`category_id` = 2要了解笔记本电脑类别的所有可用RAM大小:
SELECT DISTINCT `filters`.`value` FROM `filters` WHERE `filters`.`category_id` = 2 AND `filters`.`filter`='RAM'category_id在filters表中可以被排除,因为我们可以从products表中得到它。我增加了它,因为每次我必须获得所有过滤器,我不需要通过products表。
https://stackoverflow.com/questions/32261909
复制相似问题