获取最常用行值的%'s


Getting %'s of the most used row values

我在我的网站上跟踪访问者的国家,并使用这个mysql查询来获得顶级访问者的国家名称。

mysql_query("SELECT id,country, count(*) as num FROM track GROUP BY country ORDER BY count(*) desc limit 5")

效果很好。然而,我想要得到百分之五的国家。

这个join将报告百分比:

select id, country, num, num*100/total pct
from (SELECT id,country, count(*) as num
      FROM track GROUP BY country
      ORDER BY num desc limit 5) x
join (select count(*) total from track) y

你是指百分比吗?您可以在MySQL -中执行一些基本的数值运算http://dev.mysql.com/doc/refman/5.0/en/numeric-functions.html

您可以添加另一条语句,该语句调用所有行的COUNT(),然后使用除法运算符来获得百分比,如果您希望在同一个查询中这样做的话。