MySQL-如何为一种产品存储多种颜色组合?
我正在寻找以下问题的最佳解决方案:
我有三张桌子:
- 产品
ID | ProductName |
---|---|
1 | T恤 |
2 | 套套 |
3 | 夹克 |
- 基本颜色
ID | basecolorname | washe lasextracolorids |
---|---|---|
1 | green | 1,2 |
2 | 蓝色 | 2,3 |
3 3 3 | 红色 | 3 |
- 红色额外的颜色
ID ID | ExtracolOrName |
---|---|
1 | Purple |
2 | 橙色 |
3 | 黑色, |
如您所见,桌子底座和额外的颜色之间存在依赖性。
- 因此,每种基本颜色都不能使用每种额外的颜色。对于基本颜色“绿色”,仅带有IDS“ 1和2”的额外颜色可以选择。
现在,表产品包含产品。但是,我现在想拥有一种表格,用户可以首先选择产品,然后他可以将基础和额外颜色的组合映射到该产品中。
示例:
- 用户现在选择“ T恤”作为产品
- 创建组合:
- 他选择了基本颜色“绿色”,他选择了允许的额外颜色“紫色”,
- 他选择了基本的“红色红色”他选择了允许的额外颜色“黑色”
,现在我想知道如何保存此信息,因此在MySQL数据库中为单个产品的不同基础组合和额外颜色的映射映射。
有人知道如何实现这一目标吗? :)
I am searching for the best solution for the following issue:
I have three tables:
- Product
id | productName |
---|---|
1 | Tshirt |
2 | Pullover |
3 | Jacket |
- Base Color
id | baseColorName | allowedExtraColorIds |
---|---|---|
1 | Green | 1,2 |
2 | Blue | 2,3 |
3 | Red | 3 |
- Extra Color
id | extraColorName |
---|---|
1 | Purple |
2 | Orange |
3 | Black |
So as you can see, there is a dependency between the tables Base and Extra Color.
- So not each Extra Color can be used with each Base Color. For the Base Color "Green" only the Extra Colors with the Ids "1 and 2" are allowed to pick.
Now the Table Product contains the products. However I now want to have a form where the user can first select the product and then he can map combinations of Base and Extra Colors to this product.
Example:
- The user selects the "Tshirt" as a product
- Now he creates Combinations:
- He selects the Base Color "Green" and he selects the allowed Extra Color "Purple"
- He selects the Base Color "Red" and he selects the allowed Extra Color "Black"
And now I am wondering how to save this information, so the mapping of the different combinations of Base and Extra Colors for the single product in a MySql Database.
Has anyone an idea how to achieve this? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须创建下面的桌子才能存储生成的组合。
selected_products
selected_product_combinations
在这里,Selected_product_id是对Selected_products表的主要键的外键引用。
You have to create tables like below to store generated combinations.
selected_products
selected_product_combinations
Here, selected_product_id is a foreign-key reference to the primary key of selected_products table.
详细说明@riggsfolly已经建议的内容:
像这个
(允许组合)或不允许(不允许组合),
这会改变您的方法,但是如所说,这就是您使用RDBMS做事的方式。之后,它将为您节省很多头痛。
是的,要提出所有可能的组合,您将检索结果集而不是一个记录,但很容易解决。
Elaborating a bit on what @RiggsFolly already suggested:
Like this
That way, all checks become a simple singleton query that either gives a result (combination allowed) or doesn't (combination not allowed)
This will change your approach a bit but as said, that's how you do things with RDBMS's. It will save you a lot of headaches afterwards.
Yes, to propose all possible combinations you will retrieve a result set instead of one record, but that is easily solved.