标签: mysql
是否有简单的查询来实现以下目标?
我有下表: ID RATING 1 1 2 2 3 5 4 5
ID RATING 1 1 2 2 3 5 4 5
我需要得到每个评级的百分比 1 25 2 25 5 50
1 25 2 25 5 50
答案 0 :(得分:3)
以下代码对您有所帮助,
SELECT RATING, (COUNT(*) / (SELECT COUNT(*) FROM your_table)) * 100 AS Percentage FROM your_table GROUP BY RATING;
SQL Fiddle Demo