Транспонировать Mysql запросы для Magento - PullRequest
0 голосов
/ 07 марта 2012

Я хочу транспонировать результат следующего запроса, который дает приведенный ниже результат.

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

У меня естьнаписал довольно длинный вложенный запрос выбора, который дает мне желаемый результат.Есть ли лучший / более простой способ получить запрос для Output2, если у меня есть запрос для Output 1.

// Edit 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 ]

1 голос
/ 07 марта 2012

Нечто подобное.Это, безусловно, может потребовать корректировки.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 
0 голосов
/ 08 марта 2012

Это то, что я искал Сводная таблица .Используя SUM / GROUP_CONCAT вместе с IF, можно транспонировать таблицу

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...