使用 MySQL 的产品功能选择器

发布于 2024-11-17 12:36:20 字数 895 浏览 3 评论 0原文

如下所示,产品 (id 100) 与 ManSM 变体相关。

功能表:

 feature_id      name
------------  ----------
     1          Gender
     2           Size

功能变体表:

 variant_id      feature_id        name
------------    -------------    --------
     1                1             Man
     2                1            Woman
     3                2              S
     4                2              M
     5                2              L

产品功能关系表:

 product_id      feature_id        variant_id
------------    -------------     ------------
    100               1                1
    100               2                3
    100               2                4

我如何使用将这三个变体与产品相关联 MySQL 命令?我真的没有这么做。

As you see below, product (id 100) related with Man, S and M variants.

Features table:

 feature_id      name
------------  ----------
     1          Gender
     2           Size

Feature variants table:

 variant_id      feature_id        name
------------    -------------    --------
     1                1             Man
     2                1            Woman
     3                2              S
     4                2              M
     5                2              L

Product Feature relation table:

 product_id      feature_id        variant_id
------------    -------------     ------------
    100               1                1
    100               2                3
    100               2                4

How can I relate these three variants with product using MySQL command? I really didn't do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

愁以何悠 2024-11-24 12:36:20

首先,您不必要地重复了一些信息。您的产品功能关系表中不需要 feature_id。这可以来自您的功能变体表。如果您要添加新的产品属性,则必须复制此信息。此外,您可能会陷入不一致的状态。如果您的某些报告依赖于一个表中的功能 ID,而另一个报告则使用另一个表,并且您忘记更新其中一个报告,则您的报告可能仍显示错误信息。

要回答您的问题,您只需加入各个表即可。

select pf.product_id, f.name, fv.name 
  from       productfeatures pf 
  inner join featurevariants fv   on pf.variant_id = fv.variant_id
  inner join features             on fv.feature_id = f.feature_id

您的示例中没有产品表,但如果有,您也可以加入该表并获取产品的名称而不是 ID(如果您愿意)。

希望这有帮助!

First, you are repeating some information unnecessarily. You don't need feature_id in your product feature relation table. That can come from your feature variants table. If you are adding new product attributes, you'll have to duplicate this information. Also, you can get into an inconsistent state. If some of your reports rely on feature id from one table and another uses the other table, and you forget to update one of them, you can have a report that is still showing the wrong information.

To answer your quesion, you simply join across the tables.

select pf.product_id, f.name, fv.name 
  from       productfeatures pf 
  inner join featurevariants fv   on pf.variant_id = fv.variant_id
  inner join features             on fv.feature_id = f.feature_id

You don't have a product table in your example, but if you did, you could also join that and get the name of the product instead of the id, if you wanted to.

hope this helps!

柠北森屋 2024-11-24 12:36:20

您尚未显示“产品”表,但我假设有一个。

SELECT * FROM product p INNER JOIN product_feature_relation rel ON p.product_id = rel.product_id INNER JOIN feature_variants fv ON rel.variant_id = fv.variant_id INNER JOIN features f ON fv.feature_id = f.feature_id

如果您要立即迭代并对其采取行动,则此查询给出的结果可能没有用。最好有 2 行代表独特的尺寸,而不是三行(2 种尺寸 + 性别) - 但您没有在问题中指定,所以我不确定您是否需要它。

You have not shown the 'product' table but I'm assuming there is one.

SELECT * FROM product p INNER JOIN product_feature_relation rel ON p.product_id = rel.product_id INNER JOIN feature_variants fv ON rel.variant_id = fv.variant_id INNER JOIN features f ON fv.feature_id = f.feature_id

The chances are the results that this query will give may not be useful if you are going to iterate and act on them straight away. It would be better to have 2 rows representing unique sizes, rather than three (2 sizes + gender) - but you didn't specify that in the question so I'm not sure that you need it.

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