简体   繁体   中英

How to order with mysql query only products which has biggest price ratio between price and promo_price

I have this query:

SELECT id,bg_product_name,product_price,promo_price FROM products 
WHERE active="1" order by ((product_price / promo_price) > 0) ASC limit 5

Sample records:

#id #bg_product_name  #product_price  #promo_price
74  Jewellery         30.00            28.00
75  Jewellery1        20.00            0.00
76  Jewellery2        30.00            25.00

and so on. My query returns records but they are not sorted by biggest price difference.

Full query:

SELECT 
p.id, bg_product_name, unique_name, product_price, promo_price,
 promo_end_date, available_qty, model, p.added_date, url, bg_category, free_delivery, 
free_option,supplier_name,link_name, (product_price / promo_price) as  p_ratio 
FROM 
products p, products_to_categories ptc, products_to_adverts pta, products_categories pc, adverts a 
WHERE 
p.id=ptc.product_id and ptc.category_id="156" and ptc.category_id=pc.id
 and p.active="1" having p_ratio > 0 and p.instock="1" and p.id=pta.product_id and
 available_qty > 0 AND pta.advert_id=a.id AND 
pta.advert_id not in (0) 
GROUP BY ptc.product_id order by p_ratio DESC,p.updated_date DESC LIMIT 5

You might try this :

SELECT id,bg_product_name,product_price,promo_price 
FROM products 
WHERE active="1" 
ORDER BY ABS(product_price - promo_price) ASC
LIMIT 5

Instead of a HAVING clause as in the accepted answer you can use a WHERE clause, which in most cases is better performance-wise ...under one condition: There are no negative (promo_)prices (a reasonable assumption, I think?)
In that case there is only one value you have to take care of: promo_price==0 as in your Jewellery1 example. The "ratio" for that record would be 20.0/0.0 -> NULL. Any other value for promo_price will result in a positive number for the ratio.
So, just filter out records with promo_price=0.0 before something else is done.

SELECT
    id,bg_product_name,product_price,promo_price, (product_price/promo_price) as ratio
FROM
    products 
WHERE
    active="1"
    AND promo_price > 0
ORDER BY
    ratio ASC
LIMIT
    5

Or maybe even better: Also keep the records having promo_price=0 in the list, but assign a distinct value to ratio, eg

SELECT
    id,bg_product_name,product_price,promo_price,
    if(promo_price>0, product_price/promo_price, 'free as in beer') as ratio
FROM
    products 
WHERE
    active="1"
ORDER BY
    ratio ASC
LIMIT
    5

Try this code

SELECT id, bg_product_name, product_price, promo_price, (product_price / promo_price) as p_ratio  FROM products 
WHERE active="1" GROUP BY products.id having p_ratio > 0 order by p_ratio ASC limit 5

order must be DESC

try this:add (product_price / promo_price) to select clause then do order by. add ratio > 0 in where clause if you don't need 0 .

SELECT id,bg_product_name,product_price,promo_price,(product_price / promo_price) as ratio FROM products 
WHERE active="1" and ratio > 0 order by ratio ASC limit 5

try this

SELECT id,bg_product_name,product_price,promo_price,(product_price / promo_price) AS newDiff FROM products 
WHERE active="1" order by newDiff) ASC limit 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM