MySQL求百分比带百分号%

方式一:

round() + concat()     推荐

concat(  round((a.price-b.price)/b.price*100 ,2)  ,'%')
concat(  round((a.price-b.price)/b.price*100 ,3)  ,'%')

-> (a.price-b.price)/b.price = 0.234467  -->  23.45%
-> (a.price-b.price)/b.price = 0.234467  -->  23.447%

示例:

select c.* , d.* ,
	concat( round((c.sale2020-d.sale2019)/d.sale2019*100,3), '%') '销售额同比增长' 
	from	
	(select convert(sum(o.product_amount / 100 ) , decimal(10,2)) as 'sale2020' from `order` o
		where o.begin_date between '2020-09-10 00:00:00' and '2020-09-30 23:59:59') c ,	
	(select convert(sum(o.product_amount / 100 ) , decimal(10,2)) as 'sale2019' from `order` o
		where o.begin_date between '2019-09-10 00:00:00' and '2019-09-30 23:59:59') d

方式二:

left() + concat()

concat(  left((a.price-b.price)/b.price*100 ,6)  ,'%')
concat(  left((a.price-b.price)/b.price*100 ,4)  ,'%')

-> (a.price-b.price)/b.price = 0.234467  -->  23.446%
-> (a.price-b.price)/b.price = 0.234467  -->  23.4%

示例:

select c.* , d.* ,
	concat( left((c.sale2020-d.sale2019)/d.sale2019*100,5), '%') '销售额同比增长' 
	from	
	(select convert(sum(o.product_amount / 100 ) , decimal(10,2)) as 'sale2020' from `order` o
		where o.begin_date between '2020-09-10 00:00:00' and '2020-09-30 23:59:59') c ,	
	(select convert(sum(o.product_amount / 100 ) , decimal(10,2)) as 'sale2019' from `order` o
		where o.begin_date between '2019-09-10 00:00:00' and '2019-09-30 23:59:59') d	

 
 

round(数字 , 小数点后位数)
取小数位四舍五入

left(被截取字段,截取长度)
从左向右截取字符串(小数点也算一个字符)

concat(内容1 , 内容2 , … 内容n) 拼接

猜你喜欢

转载自blog.csdn.net/C2667378040/article/details/108872699