Home >Backend Development >PHP Tutorial >百分比运算,然后排列的有关问题

百分比运算,然后排列的有关问题

WBOY
WBOYOriginal
2016-06-13 10:32:081043browse

百分比运算,然后排列的问题。
有表 tab 字段 a1 a2 

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->a1  a2          王   0                王   1          王   3          李   1          李   2          


运算 a2 里 王>0 的数值有2个 王的数值一共有3个 2/3 输出66%
李 是 100% 然后按照百分比大小 按从大到小排列


输出结果显示
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->李   100%王    66%


------解决方案--------------------
select a1,sum(if(a2>0,1,0))/count(*)*100 as num from tab group by a1 order by num desc

式中:
sum(if(a2>0,1,0)) 统计 a2 大于 0 的个数
count(*) 统计总体的个数

大于0的个数 / 总体个数 * 100
就是
大于0的 占 总体 的百分数,这是基本的算术
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn