首页 > 解决方案 > 如何更正 MySQL 多个 JOINS 查询中的列名值

问题描述

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

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

这是我目前收到的:

当前结果

该查询部分正确,因为:

问题是,虽然股票信息是正确的,但 ///信息id_attribute是错误的。attributeid_detaildetail

这是我需要收到的(我已将更正标记为粗体):

修正结果

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

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

标签: mysqljoingroup-byleft-joinmysql-5.6

解决方案


正如你提到的:

产品有库存。该库存与颜色 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 |
+------------+--------------------------+-------+-----------+----------+-------+--------------+--------+--------------+-----------+-----------+-----------+

推荐阅读