mysql 计算字段/字段(百分比)

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

查询结果:

mysql 计算字段/字段(百分比)_第1张图片

你可能感兴趣的:(随笔,mysql,字段百分比,运算)