MySQL显示结果两次给定不同的条件

时间:2016-10-20 16:47:23

标签: mysql magento

我有以下代码用于从Magento数据库中获取价格:

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,经销商价格,交易商价格

由于

1 个答案:

答案 0 :(得分:0)

You just need left joins instead of inner joins. Note in the tested example below that I'm renaming the alias of price_index to price_index2 in the second join on the same table.

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