2012-03-07 13 views
-2

私は以下のクエリの結果を転置することを検討しています。Magento用のTranspose Mysqlクエリ

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 

私は私が私の望ましい結果を与えるかなり長いネストされたSELECTクエリを書かれている次の出力結果

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

を達成しようとしています。 Output2のクエリを取得するためのより良い/簡単な方法はありますか?これは価格と重量の2つの属性に対してのみです。

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' 
+1

ネストしたクエリを投稿して、最適化できるかどうかを確認してください。基本的には、レポートのために独自のネストされたクエリーを書くことが最善の方法です。アプリケーション(ビジネスロジックなど)でデータを使用している場合は、Magento ORM(Zend DB)を使用して後で結果をソートする必要があります。 –

+0

質問に私の質問を追加しました。 – rdsoze

+0

最初はRubyでMagento Core Apiを使用しようとしましたが、APIコールは遅すぎました。私はxmlrpc/soapコールを作るためのsavonとmagentorの宝石にも問題がありました。だから私は質問レベルに下がることを考えた。 – rdsoze

答えて

0

これは私がPivot Table探していたものです。 IFとともにSUM/GROUP_CONCATを使用すると、テーブルを転置できます

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