Question

I'm not sure if this is possible. But I have the following sql that inserts some values to the product_filter table based on what the special price of a product is.

INSERT INTO product_filter (product_id,filter_id)
SELECT product_id,
  (CASE WHEN price < 100 then 1
  WHEN price >= 100 AND price < 500 then 2
  WHEN price >= 500 AND price < 1000 then 3
  WHEN price >= 1000 AND price < 1500 then 4
  WHEN price >= 1500 AND price < 2000 then 5
  WHEN price >= 2000 AND price < 2500 then 50
  WHEN price >= 2500 AND price < 3000 then 6
  ELSE 51 END) AS filter_id
FROM product_special;

I'd like to modify the above script so if there is no special price set for a product, it just reads the regular price from the main product table. Since this will be run on a large database of products, I'd like to possibly combine both scripts instead of me manually running a different script on certain products.

Thanks for any help.

Was it helpful?

Solution

INSERT INTO product_filter (product_id,filter_id)

  SELECT product_id,
  (CASE WHEN price < 100 then 1
  WHEN price >= 100 AND price < 500 then 2
  WHEN price >= 500 AND price < 1000 then 3
  WHEN price >= 1000 AND price < 1500 then 4
  WHEN price >= 1500 AND price < 2000 then 5
  WHEN price >= 2000 AND price < 2500 then 50
  WHEN price >= 2500 AND price < 3000 then 6
  ELSE 51 END) AS filter_id
  FROM product_special
  union
  SELECT product_id,
  (CASE WHEN price < 100 then 1
  WHEN price >= 100 AND price < 500 then 2
  WHEN price >= 500 AND price < 1000 then 3
  WHEN price >= 1000 AND price < 1500 then 4
  WHEN price >= 1500 AND price < 2000 then 5
  WHEN price >= 2000 AND price < 2500 then 50
  WHEN price >= 2500 AND price < 3000 then 6
  ELSE 51 END) AS filter_id
  FROM product where product_id not in
  (select product_id from product_special)

The above query assumes that you have product_id and price (as standard/regular price) columns in the product table. The idea here is to get all the existing products from the product_special table and their special prices and to adds to it the products that are not there in the product_special table but are there in the product table along with their standard/regular prices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top