Jacques
Jacques

Reputation: 117

How to select products where price is 20% higher or lower?

I have a "similar products" query, where I select products from the same category.

I now have to refine it further to select products in the category, that are within 20% above or below the related product.

Here is what I have:

SELECT p.name AS product_name, 
p.product_link, 
p.price AS price 
FROM product p 
JOIN category_product cp ON p.id_product = cp.id_product 
JOIN category c ON cp.id_category = c.id_category 
WHERE p.active = 1 
AND c.id_category = 4 
ORDER BY p.price DESC
LIMIT 10

I simply have no idea how to add a condition for the percentage (include all products where the price is 20% below to 20% above my price, say $25 )

Could you show me how you would go about this?

Upvotes: 0

Views: 243

Answers (1)

trincot
trincot

Reputation: 350137

You can just add that in the where clause providing the following formula:

where p.price / 25 - 1 between -0.20 and 0.20

Replace 25 with whatever you want to compare with.

Upvotes: 2

Related Questions