您的位置:首页 > 数据库

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

2017-10-14 10:56 363 查看
/*
查询出可配置产品(父产品)、子产品的价格,和与子产品对应属性的差价

在我们的项目中
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: