Reputation: 117
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
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