round( 字段 / 字段 , 保留小数位数)
注意: COUNT(p.id)/c.sum 计算时,不能使用别名代替,否则报错(全部数学运算都是)
select
COUNT(p.id) as package_count,
c.sum,
ROUND(COUNT(p.id)/c.sum,3) as proportion
from t_order o, t_package p, (select COUNT(1) SUM from t_order) c
where o.package_id = p.id group by p.id order by package_count desc limit 0,4
查询结果:
百分比:
concat(字段,'%') 先round计算数值,再拼接 %
select
COUNT(p.id) as package_count,
c.sum,
CONCAT(ROUND(COUNT(p.id)/c.sum * 100 ,3),'%') as proportion
from t_order o, t_package p, (select COUNT(1) SUM from t_order) c
where o.package_id = p.id group by p.id order by package_count desc limit 0,4
查询结果: