简体   繁体   English

如何使用mysql查询仅查询价格和promo_price之间价格比最大的产品

[英]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?) 您可以使用WHERE子句,而不是在接受的答案中使用HAVING子句,该子句在大多数情况下在性能上更佳……在一种情况下:没有负(促销)价格(我认为是合理的假设吗?)
In that case there is only one value you have to take care of: promo_price==0 as in your Jewellery1 example. 在这种情况下,您只需要照顾一个值:promo_price == 0,如您的Jewellery1示例中所示。 The "ratio" for that record would be 20.0/0.0 -> NULL. 该记录的“比率”为20.0 / 0.0-> NULL。 Any other value for promo_price will result in a positive number for the ratio. promo_price的任何其他值都将导致比率为正数。
So, just filter out records with promo_price=0.0 before something else is done. 因此,只需在执行其他操作之前过滤掉具有promo_price = 0.0的记录即可。

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 甚至可能更好:将列表中具有promo_price = 0的记录保留在列表中,但为比率分配一个不同的值,例如

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 订单必须为DESC

try this:add (product_price / promo_price) to select clause then do order by. 试试这个:添加(product_price / promo_price)选择子句,然后进行排序。 add ratio > 0 in where clause if you don't need 0 . 如果不需要0 ,则在where子句中添加ratio > 0 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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