【问题标题】:MYSQL - Pricing filter for products with and without child rowsMYSQL - 具有和不具有子行的产品的定价过滤器
【发布时间】:2019-01-24 11:51:18
【问题描述】:

好的,所以我有一个存储所有产品的表。产品分为三类; “简单”、“可配置”和“变体”。

Simple 和 Variant 产品只有一行,其中有一个“price”列和一个可选的“sale_price”列。

可配置产品的“price”和“sale_price”列的值为零,但具有子(变体)产品行(由“parent_id”列连接)。

我想做的是根据价格创建一个过滤器。如果产品具有“sale_price”值,则应使用该值而不是“price”值。这适用于简单的产品:

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 之间。

提前致谢。我完全坚持这一点。我已经尝试过内部连接,但我的语法不太正确。

【问题讨论】:

标签: mysql database join sum inner-join


【解决方案1】:

我假设您在表格中有以下列

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)
      ));

【讨论】:

  • 为您的不懈努力表示敬意。那就是显示伟人的伟大价值。分享是关怀。继续前进
  • 酷,好的,谢谢您的回复。我希望返回父级可配置而不是变体本身,但这给了我一些工作要做。感谢您的帮助。
  • @StewDellow 我很高兴能帮上忙。如果我的回答对您有帮助,您应该接受它并选择支持:)
猜你喜欢
  • 2014-11-26
  • 2013-10-20
  • 2017-04-16
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
相关资源
最近更新 更多