2016-10-20 11 views
0

さまざまな条件を考えると、私はMagentoのデータベースから価格を取得するために使用している次のコードを持っている:MySQLの表示結果は二度

SELECT 
    `e`.`sku`, 
    `price_index`.`price` AS `RRP`, 
    `price_index`.`final_price` AS `Dealer Price` 

FROM 
    `catalog_product_entity` AS `e` 

INNER JOIN 
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND 
price_index.website_id = '1' AND price_index.customer_group_id = 7 

これは正常に動作しますが、しかし、私はfinal_price」から追加の価格を取得する必要があります顧客グループ8ではなく、例えば7である。

私の現在のスクリプトの表示からの結果: SKU、RRP、ディーラー価格

私が望む結果: SKU、RRP、ディーラー価格、トレーダー価格

おかげ

答えて

0

あなただけ内部結合の代わりに左結合が必要です。以下のテスト例では、同じテーブルの2番目の結合でprice_indexの別名をprice_index2に変更しています。また

SELECT 
    `e`.`sku`, 
    `price_index`.`price` AS `RRP`, 
    `price_index`.`final_price` AS `Dealer Price`, 
    `price_index2`.`final_price` AS `Trader Price` 

FROM 
    `catalog_product_entity` AS `e` 

LEFT JOIN 
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND 
price_index.website_id = '1' AND price_index.customer_group_id = 7 

LEFT JOIN 
`catalog_product_index_price` AS `price_index2` ON price_index2.entity_id = e.entity_id AND 
price_index2.website_id = '1' AND price_index2.customer_group_id = 8 
+0

同じテーブルを複数回接合の参考のために良い例:http://stackoverflow.com/questions/199953/how-do-you-join-on-the-same-table-twice- in-mysql –

+0

完璧な、迷惑なものは、私はこれに非常に類似した何かを試してみました。ありがとうございました! –