我需要用特定的颜色和属性来显示一个产品的价格,以及该产品的当前库存(如果有的话)。它做了它应该做的,部分。这是查询的解释(或者至少是它应该做的)。
store_product
对于这个特殊的例子,我试图接收产品ID的库存值。59
满足这些具体价值的
id_color
必须 1
(1 = 红色,它有一个自定义的名字叫 红煞星)id_detail
必须 4
一行,而 50
在另一行(4
= 尺寸M 50
= 材料 Poliester)这是我目前收到的信息。
查询是部分正确的,因为:
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 |
+------------+--------------------------+-------+-----------+----------+-------+--------------+--------+--------------+-----------+-----------+-----------+