[Magento SQL]查询出可配置产品(父产品)、子产品的价格,和与子产品对应属性的差价

/*
查询出可配置产品(父产品)、子产品的价格,和与子产品对应属性的差价

在我们的项目中
attribute_id=76是为了查询产品的special price价格;
parent_id=1459我们查询是id为1459的这个可配置产品
*/

SELECT t1.child_id,t2.value,t3.value as c_price,t4.pricing_value,t4.value as p_price,t5.sku
FROM catalog_product_relation AS t1
LEFT JOIN catalog_product_entity_int AS t2 ON t2.entity_id=t1.child_id
LEFT JOIN catalog_product_entity_decimal AS t3 ON t3.entity_id=t1.child_id
left join (
    SELECT t1.product_id,t1.attribute_id, t2.value_index,t2.pricing_value,t3.value
    FROM catalog_product_super_attribute AS t1
    LEFT JOIN catalog_product_super_attribute_pricing AS t2 ON t2.product_super_attribute_id=t1.product_super_attribute_id
    LEFT JOIN catalog_product_entity_decimal AS t3 ON t3.entity_id=t1.product_id
    WHERE t1.product_id=1459 AND t2.pricing_value IS NOT NULL AND t3.attribute_id=76
) as t4 on t4.value_index=t2.value
LEFT JOIN catalog_product_entity AS t5 ON t5.entity_id=t1.child_id
WHERE t1.parent_id=1459 AND t3.attribute_id=76 
AND t2.attribute_id IN (SELECT attribute_id FROM catalog_product_super_attribute WHERE product_id=1459)
 AND t2.value IN (SELECT  t2.value_index FROM catalog_product_super_attribute AS t1
LEFT JOIN catalog_product_super_attribute_pricing AS t2 ON t2.product_super_attribute_id=t1.product_super_attribute_id
WHERE t1.product_id=1459);

这样就查询出了 差价pricing_value、父产品价格p_price、子产品价格c_price 和 子产品SKU。
要注意产品和对应属性差值的组合:n种组合查询的总记录也是n。

#最后如果需要 “差价+父产品价格!=子产品价格” 的记录
#也就是在上面结果集上还需要多一层处理
#可以用到HAVING ,但HAVING 之前必须有GROUP BY

#这里我们要所有字段联合起来 作为GROUP BY的字段,利用msyql的CONCAT()函数
SELECT t1.child_id,t2.value,t3.value as c_price,t4.pricing_value,t4.value as p_price,t5.sku,CONCAT(t1.child_id,t2.value,t3.value,t4.pricing_value,t4.value,t5.sku) as g
FROM catalog_product_relation AS t1
LEFT JOIN catalog_product_entity_int AS t2 ON t2.entity_id=t1.child_id
LEFT JOIN catalog_product_entity_decimal AS t3 ON t3.entity_id=t1.child_id
left join (
    SELECT t1.product_id,t1.attribute_id, t2.value_index,t2.pricing_value,t3.value
    FROM catalog_product_super_attribute AS t1
    LEFT JOIN catalog_product_super_attribute_pricing AS t2 ON t2.product_super_attribute_id=t1.product_super_attribute_id
    LEFT JOIN catalog_product_entity_decimal AS t3 ON t3.entity_id=t1.product_id
    WHERE t1.product_id=1459 AND t2.pricing_value IS NOT NULL AND t3.attribute_id=76
) as t4 on t4.value_index=t2.value
LEFT JOIN catalog_product_entity AS t5 ON t5.entity_id=t1.child_id
WHERE t1.parent_id=1459 AND t3.attribute_id=76 
AND t2.attribute_id IN (SELECT attribute_id FROM catalog_product_super_attribute WHERE product_id=1459)
 AND t2.value IN (SELECT  t2.value_index FROM catalog_product_super_attribute AS t1
LEFT JOIN catalog_product_super_attribute_pricing AS t2 ON t2.product_super_attribute_id=t1.product_super_attribute_id
WHERE t1.product_id=1459) GROUP BY g;
#在上面结果集的基础之上判断:差价+父产品价格!=子产品价格
#

HAVING pricing_value+p_price!=c_price

如果最后结果是只是需要子产品的SKU,可以在套一层查询,然后GROUP BY 就去掉重复的子产品SKU了:

SELECT sku from 
(
# 上面的SQL
) AS tmp GROUP BY sku;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值