MySQL-如何为一种产品存储多种颜色组合?

发布于 2025-02-01 02:14:20 字数 1310 浏览 4 评论 0原文

我正在寻找以下问题的最佳解决方案:

我有三张桌子:

  1. 产品
IDProductName
1T恤
2套套
3夹克
  1. 基本颜色
IDbasecolornamewashe lasextracolorids
1green1,2
2蓝色2,3
3 3 3红色3
  1. 红色额外的颜色
ID IDExtracolOrName
1Purple
2橙色
3黑色,

如您所见,桌子底座和额外的颜色之间存在依赖性。

  • 因此,每种基本颜色都不能使用每种额外的颜色。对于基本颜色“绿色”,仅带有IDS“ 1和2”的额外颜色可以选择。

现在,表产品包含产品。但是,我现在想拥有一种表格,用户可以首先选择产品,然后他可以将基础和额外颜色的组合映射到该产品中。

示例:

  1. 用户现在选择“ T恤”作为产品
  2. 创建组合:
  • 他选择了基本颜色“绿色”,他选择了允许的额外颜色“紫色”,
  • 他选择了基本的“红色红色”他选择了允许的额外颜色“黑色”

,现在我想知道如何保存此信息,因此在MySQL数据库中为单个产品的不同基础组合和额外颜色的映射映射。

有人知道如何实现这一目标吗? :)

I am searching for the best solution for the following issue:

I have three tables:

  1. Product
idproductName
1Tshirt
2Pullover
3Jacket
  1. Base Color
idbaseColorNameallowedExtraColorIds
1Green1,2
2Blue2,3
3Red3
  1. Extra Color
idextraColorName
1Purple
2Orange
3Black

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:

  1. The user selects the "Tshirt" as a product
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2025-02-08 02:14:20

您必须创建下面的桌子才能存储生成的组合。

  1. selected_products

    idproduct_iduser_id
    111
    221
  2. selected_product_combinations

    idselected_product_idbase_color_idextra_color_id
    1111
    2133

在这里,Selected_product_id是对Selected_products表的主要键的外键引用。

You have to create tables like below to store generated combinations.

  1. selected_products

    idproduct_iduser_id
    111
    221
  2. selected_product_combinations

    idselected_product_idbase_color_idextra_color_id
    1111
    2133

Here, selected_product_id is a foreign-key reference to the primary key of selected_products table.

度的依靠╰つ 2025-02-08 02:14:20

详细说明@riggsfolly已经建议的内容:

  • 使用1个颜色表。拥有两个只会使情况复杂化
  • 创建一个表格,其中所有可能的组合都列为单个项目 - 您甚至可以考虑添加产品以允许场景,而所有产品都不适用于所有颜色。

像这个

产品的碱基外表
111
12那样,所有检查都是一个简单的单例查询,要么给出结果

(允许组合)或不允许(不允许组合),

这会改变您的方法,但是如所说,这就是您使用RDBMS做事的方式。之后,它将为您节省很多头痛。

是的,要提出所有可能的组合,您将检索结果集而不是一个记录,但很容易解决。

Elaborating a bit on what @RiggsFolly already suggested:

  • work with 1 table of colors. Having two of them will only complexify the situation
  • create a table where all possible combinations are listed as individual items - you could even consider adding the product to allow scenarios where not all colors are available for all products.

Like this

ProductBasecolorExtracolor
111
112

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文