mysql-pricing过滤器,用于有或无子行的产品

9q78igpj  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(279)

好的,我有一张table,上面放着所有的产品。产品有三种类型“简单”、“可配置”和“变体”。
简单产品和变型产品只是一行,有一个“价格”列和一个可选的“销售价格”列。
可配置产品的“price”和“sale\u price”列的值为零,但却有子(variant)产品行(由“parent\u id”列连接)。
我要做的是根据价格创建一个过滤器。如果一个产品有一个“销售价格”值,它应该用它来代替“价格”值。这适用于简单产品:

select `products`.*, 
`lookup_product_categories`.`category_id` as `pivot_category_id`, 
`lookup_product_categories`.`product_id` as `pivot_product_id`
from `products` 
inner join `lookup_product_categories` on `products`.`id` = `lookup_product_categories`.`product_id` 
where `lookup_product_categories`.`category_id` = '38' 
and (
    `type` = 'simple' 
    and 
    IF (
        `sale_price` > 0,
        `sale_price` >= 30 AND `sale_price` <= 150,
        `price` >= 30 AND `price` <= 150
    ) 
) 
and `active` = '1' 
and `valid` = '1' 
and `products`.`deleted_at` is null 
order by `created_at` desc

我现在需要做的是在“可配置”产品的情况下获取子产品,确定每个产品是否应该使用销售价格或正常价格,然后返回父可配置产品,如果至少有一个子产品在筛选范围内,在这种情况下;在30.00到150.00之间。
提前谢谢。我完全被这个困住了。我试过内部连接,但语法不太正确。

knsnq2tg

knsnq2tg1#

我假设表中有以下列

id | type | sale_price | price | parent_id

如果没有示例数据,很难编写查询,但您可以尝试以下操作:

select * from my_table t1
where (type in ('simple', 'variant') and sale_price is not null and sale_price between 30 and 150)
   or (type in ('simple', 'variant') and sale_price is null and price between 30 and 150)
   or (type = 'configurable' and exists(
        select 1 from my_table
        where (t1.parentid = id and sale_price is not null and sale_price between 30 and 150)
           or (t1.parentid = id and sale_price is null and price between 30 and 150)
      ));

相关问题