基于两个数字的百分比变化,总数未知(MySQL)
我有一个MySQL数据库,包含正常价格和折扣价格。我试图计算从这些数字中的最高值到当前最低值的百分比变化 当正常价格低于折扣价格时,百分比不正确 代码: 我如何调整计算以考虑哪一个是当前总数 我想我需要更改计算或有条件地比较最高/最低的两个数字,并动态更改选择?有什么想法吗 样本数据:基于两个数字的百分比变化,总数未知(MySQL),mysql,Mysql,我有一个MySQL数据库,包含正常价格和折扣价格。我试图计算从这些数字中的最高值到当前最低值的百分比变化 当正常价格低于折扣价格时,百分比不正确 代码: 我如何调整计算以考虑哪一个是当前总数 我想我需要更改计算或有条件地比较最高/最低的两个数字,并动态更改选择?有什么想法吗 样本数据: brand_price brand_discount_price percentage 8.7451 9.3345 6.73977427% 7.9537
brand_price brand_discount_price percentage
8.7451 9.3345 6.73977427%
7.9537 7.9538 0.00125728%
4.5832 2.2482 -50.94693664%
3.2331 11.8412 266.24911076%
例如:
品牌价格50
折扣价100
100-50/50*100=100%变化
品牌价格100
折扣价50
50-100/50*100=-100%变化=错误
基本上,这是由于计算的方式,因为我不知道如何插入更大的数字作为总数,我认为百分比不是我擅长的东西。如果我不太确定为什么常规价格会低于折扣价格,只需使用一个:
SELECT brand_price, brand_discount_price,
IF(brand_discount_price > brand_price, ((brand_discount_price - brand_price)/brand_discount_price) * 100, ((brand_price - brand_discount_price)/brand_price)*100) as percentage
FROM brand
如果我不太清楚为什么常规价格会低于折扣价格,请简单使用:
SELECT brand_price, brand_discount_price,
IF(brand_discount_price > brand_price, ((brand_discount_price - brand_price)/brand_discount_price) * 100, ((brand_price - brand_discount_price)/brand_price)*100) as percentage
FROM brand
我知道你们在价格、折扣价格和百分比方面也有问题 这里有一个例子:
$15.00 original price
- 1.50 - discount // here the discount should be lower then original price.
$13.50 sale price
我猜在你的查询中,你希望从原价中得到折扣金额的百分比,所以是这样的
SELECT round(brand_price,4) as price, round(brand_discount_price,4) as discount,
if(brand_discount_price < brand_price,
round((( brand_discount_price ) * 100) / brand_price , 2), 'discount chould be less then the original price') as percentage
FROM `brand`
我知道你们在价格、折扣价格和百分比方面也有问题 这里有一个例子:
$15.00 original price
- 1.50 - discount // here the discount should be lower then original price.
$13.50 sale price
我猜在你的查询中,你希望从原价中得到折扣金额的百分比,所以是这样的
SELECT round(brand_price,4) as price, round(brand_discount_price,4) as discount,
if(brand_discount_price < brand_price,
round((( brand_discount_price ) * 100) / brand_price , 2), 'discount chould be less then the original price') as percentage
FROM `brand`
一些样本数据可能??一些样本数据,包括你得到的和你期望得到的……你希望得到的结果是什么?具体是什么,百分比是什么意思?折扣价格占总价格的百分比或相反?一些样本数据可能?一些样本数据与您得到的和您期望的…您希望的结果是什么?什么的百分比,百分比是什么意思?折扣价格占总价格的百分比,或者相反?不应该,尽管我猜这可能是暂时的,或者如果我将一个历史最低价格与一个新的折扣价格进行比较的话。无论哪种方式,我都需要解决方案,即使这个例子是。。。奇怪:谢谢你的意见。我会试一试。@raecer请注意,我做了一个改变,以反映较高价格折扣的百分比。你在原来的帖子里似乎有一个错误,不应该是这样,尽管我想这可能是暂时的,或者如果我把一个历史最低的价格和一个新的折扣价格进行比较的话。无论哪种方式,我都需要解决方案,即使这个例子是。。。奇怪:谢谢你的意见。我会试一试。@raecer请注意,我做了一个改变,以反映较高价格折扣的百分比。你在原来的帖子里好像有个错误。