我的问题是关于百分比,我不是专家,所以我会尝试以更好的方式解释.
在我的mysql服务器中,我有一个表,比如700条记录,就像这样
+-------+---------+----------+-------+ | Name | country | language | Birth | +-------+---------+----------+-------+ | Lucy | UK | EN | 1980 | | Mari | Canada | FR | 1990 | | Gary | Canada | EN | 1982 | | Stacy | Jamaica | EN | 1986 | | Joao | Brasil | PT | 1984 | +-------+---------+----------+-------+
所以我查询1980年到1985年之间的所有记录,结果将是:
+------+---------+----------+-------+ | Name | country | language | Birth | +------+---------+----------+-------+ | Lucy | UK | EN | 1980 | | Gary | Canada | EN | 1982 | | Joao | Brasil | PT | 1984 | +------+---------+----------+-------+
从这个结果我想得到:
>这些年份之间每种语言的出现百分比
EN = 75% (3 is the total in this case) PT = 25%
>在结果表中看到的每个国家/地区的外观百分比
UK = 33% Canada = 33% Brasil = 33%
我的意思是如何在变量中转换结果以在最终函数中使用它们.
这可能有用,但有些内容如下:
原文链接:https://www.f2er.com/php/136132.htmlset @total_rows = (SELECT COUNT(*) FROM table WHERE Birth between 1980 and 1985); SELECT language,percentage FROM ( SELECT language,concat(count(language)/@total_rows,"%") AS percentage FROM table WHERE Birth between 1980 and 1985 )