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 |
至尊 | 100 | 10% |
总代 | 100 | 10% |
市代 | 300 | 30% |
分销 | 500 | 50% |