显示价格高达50%或1000%

问题描述 投票:-2回答:1

我有一张桌子:产品

|products_id | products_price | 
+------------+----------------+
|1           | 0.69           |
|2           | 1.86           |
|3           | 2.73           |

我有一张桌子:特价

|products_id | specials_new_products_price | 
+------------+-----------------------------+
|1           | 27.40                       |
|2           | 37.94                       |
|3           | 1.91                        |

在我的选择中,我需要找到哪些产品的specials_new_products_price高达50%或1000%。

编辑(来自评论):

select p.price, s.specials_new_products_price
from products, specials
where s.specials_new_products_price ...

是否可以添加查询此((0.69 - 27.4)/ 0.69)* 100,当结果达到例如-50告诉我这个产品

mysql sql percentage
1个回答
0
投票

我想你只需要将你的公式添加到WHERE子句中并将硬编码的数字转换为字段名称:

select 
  p.products_id,
  p.products_price, 
  s.specials_new_products_price
from 
  products p
  inner join specials s 
    on p.products_id = s.products_id
where 
  ((p.products_price - s.specials_new_products_price) / p.products_price) * 100 < -50

附:您也忘了在表之间指定连接条件,我也添加了。

© www.soinside.com 2019 - 2024. All rights reserved.