嘿我已经在php和mysql中建立了一个评级系统,现在我想选择前5名,我已经做了一些东西但是如果用户增加了评级(最大值),那就是nr 1排名.
你们是如何在php和mysql中做到的?
表格如下:
- ID
- 中
- uid
- 评分
评分是1到5之间的数字
提前致谢!
正如@chaos指出的那样,这就是这个想法:
SELECT `mid`, SUM(`rating`) AS `total`
FROM `rating`
GROUP BY `mid`
ORDER BY `total` DESC
LIMIT 5
Run Code Online (Sandbox Code Playgroud)
但是,为了确保评级很少的文章不会进入前5名,您可以添加一个阈值,只允许结果中显示超过X评级的文章,或许可以更准确地显示前5名:
SELECT `mid`, SUM(`rating`) AS `total`, COUNT(1) AS `nrRatings`
FROM `rating`
GROUP BY `mid`
HAVING nrRatings > 5 // This is the threshold. Only articles with more than
// 5 ratings will be considered
ORDER BY `total` DESC
LIMIT 5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1950 次 |
| 最近记录: |