我正在寻找有关 mySQL 数据库的一些想法,以存储具有多个(可变)选项和每个产品价格的产品。例如。某些产品可能需要多个列来存储每个产品选项的详细信息:
Product 1:
SKU
Name
General product description
--------------------------
options for product 1:
Height Width Depth Weight Material Other_1 Other_2 Etc... Price
Product 2:
SKU
Name
General product description
--------------------------
options for product 2:
Flow-rate Material Weight Other_1 Other_2 Etc... Price
我有点难过,因为每种产品的列数和列名称可能不同,并且每种产品可能包含许多选项/价格。
到目前为止,我使用的是“产品”表和“价格”表:
CREATE TABLE IF NOT EXISTS `products` (
product_id int(11) NOT NULL AUTO_INCREMENT
,product_type text
,product_page_1 text
,product_page_2 text
,product_page_3 text
,product_page_4 text
,product_sku text
,product_name text
,product_head text
,product_desc text
,product_image text
,product_sequence text
,product_flags text
,product_status text
,PRIMARY KEY (product_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=102;
CREATE TABLE IF NOT EXISTS `prices` (
price_id int(11) NOT NULL AUTO_INCREMENT
,price_type text
,price_assoc text
,price_desc text
,price_option text
,price text
,price_sequence text
,price_flags text
,price_status text
,PRIMARY KEY (price_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=109;
每个产品可以有任意数量的“价格”。 “价格”通过“price_assoc”字段与产品 ID 相关联。
因此我可以为每个产品设置无数个价格条目,但是为每个价格添加多个价格选项的最佳方式是什么,因为每个产品的相关价格可能有不同的选项?
最佳答案
您正在寻找的是 EAV 表。实体属性值。 这就是你有记录表的结构,然后是子记录表。
记录表:
编号 名称 活跃
子记录表:
编号 记录编号 key 值
然后你可以做一个联合查询,在那里你得到一条记录,以及它的所有子记录。所以子记录与记录是一对多的关系。
github 中有可用的 php 示例:here
关于php - 用于容纳具有可变多个选项的产品的数据库结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792694/