mysql - 如何更正 MySQL 多个 JOINS 查询中的列名值
问题描述
我需要显示一个产品及其价格,以及该产品的当前库存(如果有)具有特定的颜色和属性。它部分地做了它应该做的事情。这是查询的解释(或至少它应该做什么):
- 从以下位置获取有关产品的基本信息
store_product
- 产品有库存。该库存与颜色 ID 和属性详细信息 ID 相关联。
- 与颜色 ID 关联的产品库存可以具有多个属性详细信息 ID。因此,例如,涤纶材料的红色衬衫库存值可能与 M 码的红色衬衫库存值不同。
对于这个特定示例,我正在尝试接收59
满足这些特定值的产品 ID 的库存值
id_color
必须是1
(1 = red,它有一个名为red deadpool的自定义名称)id_detail
必须4
在一行中,50
在另一行中(4
=尺寸 M /50
=材料涤纶)
这是我目前收到的:
该查询部分正确,因为:
- 我得到 2 行,每个颜色/属性组合一个
- 我得到了
id_stock
每一行的正确 ID,并且我还得到了颜色/属性组合的正确库存值。
问题是,虽然股票信息是正确的,但 ///信息id_attribute
是错误的。attribute
id_detail
detail
这是我需要收到的(我已将更正标记为粗体):
SELECT store_product.id_product AS id_product,
store_product.NAME AS NAME,
store_product.price AS price,
store_product.flg_stock AS flg_stock,
store_product_stock.id AS id_stock,
store_product_stock.stock AS stock,
Ifnull(store_color.NAME, color.NAME) AS color,
color.hex AS hex,
store_attribute.id_attribute AS id_attribute,
store_attribute.NAME AS attribute,
store_attribute_detail.id_detail AS id_detail,
store_attribute_detail.NAME AS detail
FROM store_product
LEFT JOIN store_product_color
ON store_product_color.id_color = store_product_color.id_color
LEFT JOIN color
ON color.id_color = store_product_color.id_color
LEFT JOIN store_color
ON store_color.id_color = color.id_color
AND store_color.id_store = 1
LEFT JOIN store_product_detail
ON store_product_detail.id_product = store_product.id_product
LEFT JOIN store_attribute_detail
ON store_attribute_detail.id_detail =
store_product_detail.id_detail
LEFT JOIN store_attribute
ON store_attribute.id_attribute =
store_attribute_detail.id_attribute
LEFT JOIN store_product_stock
ON store_product_stock.id_product = store_product.id_product
AND store_product_stock.id_color = 1
AND store_product_stock.id_detail IN( 4, 50 )
WHERE store_product.id_store = 1
AND store_product.id_product = 59
AND store_product_color.id_color = 1
GROUP BY store_product_stock.id
在这里您可以找到整个表结构和有问题的当前查询:sqlfiddle
关于如何解决问题的任何想法?谢谢!
解决方案
正如你提到的:
产品有库存。该库存与颜色 ID 和属性详细信息 ID 相关联。
在您的查询中,您的产品与您的color_id
和您的attribute_detail
. 您将您的属性直接与您的store_product
,而不是与您的color/stock
。
这意味着您的 id_attribute 和属性仅取决于store_product.id_product
但不取决于您的store_product_stock.id
编辑
首先,感谢 sqlfiddle。链接您的股票和详细信息的方法是使用store_attribute_detail.id_detail = store_product_stock.id_detail
FROM store_product
LEFT JOIN store_product_color
ON store_product_color.id_color = store_product_color.id_color
LEFT JOIN color
ON color.id_color = store_product_color.id_color
LEFT JOIN store_color
ON store_color.id_color = color.id_color
AND store_color.id_store = 1
LEFT JOIN store_product_stock
ON store_product_stock.id_product = store_product.id_product
AND store_product_stock.id_color = 1
AND store_product_stock.id_detail IN( 4, 50 )
LEFT JOIN store_attribute_detail
ON store_attribute_detail.id_detail =
store_product_stock.id_detail
LEFT JOIN store_attribute
ON store_attribute.id_attribute =
store_attribute_detail.id_attribute
LEFT JOIN store_product_detail
ON store_product_detail.id_product = store_product.id_product
+------------+--------------------------+-------+-----------+----------+-------+--------------+--------+--------------+-----------+-----------+-----------+
| id_product | NAME | price | flg_stock | id_stock | stock | color | hex | id_attribute | attribute | id_detail | detail |
+------------+--------------------------+-------+-----------+----------+-------+--------------+--------+--------------+-----------+-----------+-----------+
| 59 | Camiseta Júbilo de X-men | 55.1 | 1 | 112 | 5 | red deadpool | f44336 | 1 | Size | 4 | M |
| 59 | Camiseta Júbilo de X-men | 55.1 | 1 | 118 | 35 | red deadpool | f44336 | 8 | Material | 50 | Poliester |
+------------+--------------------------+-------+-----------+----------+-------+--------------+--------+--------------+-----------+-----------+-----------+
推荐阅读
- javascript - 铬合金
- powershell - 包装在 PSInvalidCastException 中时捕获特定异常的正确方法
- php - 如果不存在数据,则显示大小为 0 的数组,而不是大小为 1 的数组 (array(1) { [0]=> string(0) "" }) || PHP
- django - 将已批准的电子邮件点编辑到 localhost
- php - PHP:如果发生异常则撤消
- swift - 通用可解码,无需解码
- python - 如何在 keras 中重塑一维卷积层的数据?
- javascript - 找不到带有角度路径的控件
- hibernate - Hibernate @ManyToOne 总是用referencedColumnName 加载实体
- r - 来自 R 代码块的动态 RMarkdown 标题