你可以这样做:
Products:
ProductId首要的关键,
ProductName,
- ...
Materials:
MaterialID首要的关键,
MaterialName.
Units:
ProductsSizesOptions:
SizeOptionId首要的关键,
Height,
Width,
UnitId外键约束REFERENCES Units(UnitId)来处理每个产品的不同类型的单元。
ProductsMaterialOptions:
MaterialOptionId,
Quantity,
MaterialIdREFERENCES Materials(MaterialId)为每种产品的报价处理不同类型的材料的外键约束。
ProductsOffers:
OfferId首要的关键,
ProductId外键约束REFERENCES Products(ProductId),
MaterialOptionId外键约束 REFERENCES ProductsMaterialOptions(MaterialOptionId)`,
SizeOptionId外键约束REFERENCES ProductsSizesOptions(SizeOptionId),
Price.

例如,对于您在问题中发布的示例数据,您可以通过JOIN表格简单地获取每种产品的这些优惠:
SELECT
po.OfferId,
p.ProductNAme,
mo.Quantity,
m.MaterialName,
so.Height,
so.width,
u.UnitName,
po.Price
FROM products AS p
INNER JOIN ProductsOffers AS po ON p.ProductId = po.ProductId
INNER JOIN ProductsMaterialOptions AS mo ON po.MaterialOptionId = mo.MaterialOptionId
INNER JOIN ProductsSizesOptions AS so ON so.SizeOptionId = po.SizeOptionId
INNER JOIN Units AS u ON u.UnitId = so.unitId
INNER JOIN Materials AS m ON m.MaterialId = m.MaterialId;
这会给你类似的东西:
| OFFERID | PRODUCTNAME | QUANTITY | MATERIALNAME | HEIGHT | WIDTH | UNITNAME | PRICE |
-----------------------------------------------------------------------------------------
| 1 | Business Card | 100 | Card Stock | 4 | 3 | gsm | 500 |
| 2 | Business Card | 200 | Card Stock | 4 | 3 | gsm | 800 |
然后从您的前端应用程序中,您可以按照您希望的方式格式化这些结果。
更新
Products:
ProductId,
ProductName,
- ...
Options:
要存储可能的选项:
OptionId OptionName
1 Material
2 Shape
and so on
Properties:
PropertyId,
PropertyName,
PropertyTypeId.
PropertiesTypes:
PropertyTypeId,
PropertyTypeName.
您可能不需要此表,但您可以在前端应用程序中使用它来了解如何在应用程序中显示此字段。例如,它可能包含以下值:
1 Integer
2 String
3 Decimal
...
OptionsProperties:
OptionPropertyId,
OptionId,
PropertyId.
每个选项的属性,例如Shape和Material:
OptionPropertyId OptionId PropertyId
1 1 1
2 2 2
3 2 3
4 2 4
ProductOptions:
ProductOptionId,
ProductId,
OptionId.
ProductOptionsValues:
ProductOfferOptionsId,
ProductId,
PropertyId,
NumericValue,
TXTValue.
ProductsOffers:
OfferId,
ProductOfferOptionsId,
Quantity,
Price.

因此您可以获得每个产品的报价列表,如下所示:
SELECT
p.ProductName,
MAX(CASE WHEN pr.PropertyName = 'Material Name' THEN PropertyValue END) AS 'Material Name',
MAX(CASE WHEN pr.PropertyName = 'Height' THEN PropertyValue END) AS 'Height',
MAX(CASE WHEN pr.PropertyName = 'Width' THEN PropertyValue END) AS 'Width',
MAX(CASE WHEN pr.PropertyName = 'Unit' THEN PropertyValue END) AS 'Unit',
o.Quantity,
o.Price
FROM products AS p
INNER JOIN
(
SELECT
ProductId,
PropertyId,
ProductOfferOptionsId,
COALESCE(NumericValue, TXTValue) AS PropertyValue
FROM
ProductOptionsValues
) AS ov ON ov.ProductId = p.ProductId
INNER JOIN OptionsProperties AS op ON op.PropertyId = ov.PropertyId
INNER JOIN Properties AS pr ON op.PropertyId = pr.PropertyId
INNER JOIN ProductsOffers AS o ON o.ProductOfferOptionsId = ov.ProductOfferOptionsId
GROUP BY p.ProductName,
o.Quantity,
o.Price;
这会给你:
| PRODUCTNAME | MATERIAL NAME | HEIGHT | WIDTH | UNIT | QUANTITY | PRICE |
-----------------------------------------------------------------------------
| Business Card | gsm Card Stock | 3.5 | 2.5 | inch | 100 | 500 |
| Business Card | gsm Card Stock | 3.5 | 2.5 | inch | 200 | 800 |
当然,这个查询没有意义。要获取所有属性的列表,您必须使用动态 SQL 动态执行此操作。您可以将以下代码放入存储过程中:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(pr.PropertyName = ''',
pr.PropertyName, ''', ov.PropertyValue, 0)) AS ', '''', pr.PropertyName , '''')
) INTO @sql
FROM Properties AS pr
INNER JOIN
(
SELECT
PropertyId,
COALESCE(NumericValue, TXTValue) AS PropertyValue
FROM
ProductOptionsValues
) AS ov ON pr.PropertyId = ov.PropertyId;
SET @sql = CONCAT('SELECT
p.ProductName, ', @sql , ', o.Quantity,
o.Price
FROM products AS p
INNER JOIN
(
SELECT
ProductId,
PropertyId,
ProductOfferOptionsId,
COALESCE(NumericValue, TXTValue) AS PropertyValue
FROM
ProductOptionsValues
) AS ov ON ov.ProductId = p.ProductId
INNER JOIN OptionsProperties AS op ON op.PropertyId = ov.PropertyId
INNER JOIN Properties AS pr ON op.PropertyId = pr.PropertyId
INNER JOIN ProductsOffers AS o ON o.ProductOfferOptionsId = ov.ProductOfferOptionsId
GROUP BY p.ProductName,
o.Quantity,
o.Price');
prepare stmt
FROM @sql;
execute stmt;
这将动态地旋转所有属性。