使用 MySQL 的产品功能选择器
如下所示,产品 (id 100) 与 Man、S 和 M 变体相关。
功能表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不必要地重复了一些信息。您的产品功能关系表中不需要 feature_id。这可以来自您的功能变体表。如果您要添加新的产品属性,则必须复制此信息。此外,您可能会陷入不一致的状态。如果您的某些报告依赖于一个表中的功能 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.
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!
您尚未显示“产品”表,但我假设有一个。
如果您要立即迭代并对其采取行动,则此查询给出的结果可能没有用。最好有 2 行代表独特的尺寸,而不是三行(2 种尺寸 + 性别) - 但您没有在问题中指定,所以我不确定您是否需要它。
You have not shown the 'product' table but I'm assuming there is one.
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.