1、取2位小数
方式一 :
select truncate(data/total,2)
实际使用中发现丢失精度
方式二 :
select convert(data/total,decimal(10,2))
推荐此方法,数据准确
2、拼接%
select concat( truncate(1/3,2)*100 ,'%')
3、查询结果加序号
select (@i:=@i+1) i ,data ,total from mytable ,(SELECT @i:=0) as i
4、整理SQL
select
(@i:=@i+1) i,
data,
total,
concat(convert((data/total)*100,decimal(10,2)),'%') as rate
from
( select
data,
sum(data) total,
from mytable
group by mytype order by data desc
) temp,
(SELECT @i:=0) as i