MySQL 分组统计并列出百分比

SELECT
result,
n,
round( n / s * 100, 5 ) rat 
FROM
(
SELECT
* 
FROM
( SELECT result, count( 1 ) n FROM sendsms_log GROUP BY result ) t1
INNER JOIN ( SELECT count( 1 ) s FROM sendsms_log ) t2 ON 1 = 1 
) t

其中“SELECT result, count( 1 ) n FROM sendsms_log GROUP BY result”是查出短信发送结果分组统计,“SELECT count( 1 ) s FROM sendsms_log”查出所有记录数,round( n / s * 100, 5 )计算百分比,保留5位小数。效果:

result        n          rat      
至尊10010%
总代10010%
市代30030%
分销50050%

相关推荐