为Magento转置Mysql查询

时间:2012-03-07 07:24:28

标签: mysql sql magento

我希望转置以下查询的结果,该结果给出了下面提到的结果。

SELECT pro_dec.entity_id,
       attr.attribute_id,
       attr.attribute_code AS attribute_name,
       pro_dec.VALUE
FROM   `magento_eav_attribute` AS attr
       INNER JOIN magento_catalog_product_entity_decimal AS pro_dec
         ON pro_dec.attribute_id = attr.attribute_id
WHERE  attr.`entity_type_id` = 4
       AND attr.`backend_type` = 'decimal'
ORDER  BY pro_dec.entity_id 


//Output1
entity_id   attribute_id    attribute_name  value
   376      60              price   25.0000
   376      65              weight  1.0000
   377      60              price   35.0000
   377      65              weight  3.0000

我正在尝试实现以下输出结果

//Output2
entity_id   price    weight
   376      25.0000   1.0000
   377      35.0000   3.0000

我写了一个相当长的嵌套选择查询,它给了我想要的结果。鉴于我有输出1的查询,是否有更好/更简单的方法来获取Output2的查询。

//编辑1 这是我写的嵌套查询。这仅适用于价格和重量两个属性。

SELECT ent.entity_id,
       ent.type_id,
       (SELECT pro_dec.VALUE AS VALUE
        FROM   `magento_eav_attribute` AS attr
               INNER JOIN magento_catalog_product_entity_decimal AS pro_dec
                 ON pro_dec.attribute_id = attr.attribute_id
        WHERE  attr.`entity_type_id` = 4
               AND attr.`backend_type` = 'decimal'
               AND attr.attribute_id = 60
               AND pro_dec.entity_id = ent.entity_id
        ORDER  BY pro_dec.entity_id) AS price,
       (SELECT pro_dec.VALUE AS VALUE
        FROM   `magento_eav_attribute` AS attr
               INNER JOIN magento_catalog_product_entity_decimal AS pro_dec
                 ON pro_dec.attribute_id = attr.attribute_id
        WHERE  attr.`entity_type_id` = 4
               AND attr.`backend_type` = 'decimal'
               AND attr.attribute_id = 65
               AND pro_dec.entity_id = ent.entity_id
        ORDER  BY pro_dec.entity_id) AS weight
FROM   magento_catalog_product_entity AS ent 

//编辑2 这是加入查询,它再次有点膨胀,也许可以优化

SELECT ent.entity_id,
       ent.type_id,
       price.VALUE  AS price,
       weight.VALUE AS weight
FROM   magento_catalog_product_entity AS ent
       INNER JOIN (SELECT pro_dec.entity_id   AS entity_id,
                          attr.attribute_id   AS attribute_id,
                          attr.attribute_code AS attribute_name,
                          pro_dec.VALUE       AS VALUE
                   FROM   `magento_eav_attribute` AS attr
                          INNER JOIN magento_catalog_product_entity_decimal AS
                                     pro_dec
                            ON pro_dec.attribute_id = attr.attribute_id
                   WHERE  attr.`entity_type_id` = 4
                          AND attr.`backend_type` = 'decimal'
                          AND attr.attribute_id = 60
                   ORDER  BY pro_dec.entity_id) AS price
         ON price.entity_id = ent.entity_id
       INNER JOIN (SELECT pro_dec.entity_id   AS entity_id,
                          attr.attribute_id   AS attribute_id,
                          attr.attribute_code AS attribute_name,
                          pro_dec.VALUE       AS VALUE
                   FROM   `magento_eav_attribute` AS attr
                          INNER JOIN magento_catalog_product_entity_decimal AS
                                     pro_dec
                            ON pro_dec.attribute_id = attr.attribute_id
                   WHERE  attr.`entity_type_id` = 4
                          AND attr.`backend_type` = 'decimal'
                          AND attr.attribute_id = 65
                   ORDER  BY pro_dec.entity_id) AS weight
         ON weight.entity_id = ent.entity_id
WHERE  ent.type_id = 'configurable' 

2 个答案:

答案 0 :(得分:1)

这样的事情。它当然可能需要调整。 EAV导致看起来很复杂的查询:

SELECT 
       ent.entity_id,
       ent.type_id, 
       pro_dec1.value   AS price,
       pro_dec2.value   AS weight
FROM   
       magento_catalog_product_entity AS ent 

     INNER JOIN 
       magento_catalog_product_entity_decimal AS pro_dec1
         ON  pro_dec1.entity_id = ent.entity_id
     INNER JOIN
       magento_eav_attribute AS at1
         ON  at1.attribute_id = pro_dec1.attribute_id
         AND at1.entity_type_id = 4
         AND at1.backend_type = 'decimal'
         AND at1.attribute_code = 'price'

     INNER JOIN 
       magento_catalog_product_entity_decimal AS pro_dec2
         ON  pro_dec2.entity_id = ent.entity_id
     INNER JOIN 
       magento_eav_attribute AS at2
         ON  at2.attribute_id = pro_dec2.attribute_id
         AND at2.entity_type_id = 4
         AND at2.backend_type = 'decimal'
         AND at2.attribute_code = 'value'

WHERE  
       ent.type_id = 'configurable' 

ORDER BY 
       ent.entity_id 

答案 1 :(得分:0)

这就是我在寻找的Pivot Table。 使用SUM / GROUP_CONCAT和IF,可以转置表