php - 用于容纳具有可变多个选项的产品的数据库结构

标签 php mysql sql database database-design

我正在寻找有关 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/

相关文章:

email - 使用 PHP 发送邮件时 DKIM 签名未验证

mysql - 仅向用户提供 GRANT EXECUTE 'procedure'(无选择或插入权限)

php - 我使用 mysql_real_escape_string 对吗?

php - 我应该关闭 cURL 吗?

javascript - 我想更新 .json 文件,请告诉我如何操作

php - 使用 PHP 中的链接\按钮将 Mysql 数据导出到 MS Excel

php - 按最高级别 MYSQL PHP 排序(Runescape Clan 高分)

mysql - 如何连接表并显示链接表中的名称

sql - PostgreSql 中的 Select For Update 语句

mysql - 甲骨文到mysql