如何更正 MySQL 多个 JOINS 查询中的列名称值

How to correct column name values in MySQL multiple JOINS query

我需要显示产品及其价格,以及具有特定颜色和属性的该产品的当前库存(如果有)。它做了它应该做的,部分地。这是查询的解释(或至少它应该做什么):

对于这个特定示例,我正在尝试接收满足这些特定值的产品 ID 59 的库存价值

这是我目前收到的:

查询部分正确,因为:

问题是,虽然股票信息是正确的,但是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 

在这里您可以找到整个 table 结构和当前有问题的查询:sqlfiddle

关于如何解决问题的任何想法?谢谢!

如你所说:

A product has a stock. That stock is connected to a color ID, and an attribute detail ID.

在您的查询中,您的产品与您的 color_idattribute_detail 直接相关。 您将您的属性直接与您的 store_product 相关联,而不是与您的 color/stock 相关联。

这意味着您的 id_attribute 和属性仅取决于 store_product.id_product 而不是您的 store_product_stock.id

编辑 首先,感谢 sqlfiddle。 link 您的库存和详细信息的方法是使用 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 |
+------------+--------------------------+-------+-----------+----------+-------+--------------+--------+--------------+-----------+-----------+-----------+