dsdsm2016 2016-01-18 06:43
浏览 70
已采纳

MySQL查询排名中的用户数

To get the ranking of the user 3 I am using this query (It works fine):

$sql = "SELECT 
score,
FIND_IN_SET(score, 
(
SELECT GROUP_CONCAT(score 
ORDER BY score DESC)
FROM results)
) AS rank
FROM results
WHERE user_id = 3
";

Table structure:

user_id - score

Now I want to give the user 3 some points. My formule is:

Number of score (Or users) - rank + 1.

So if I have 20 users (Or score) and the ranking of the user 3 is 10, so the points will be:

20 - 10 + 1 = 11 points.

PS: users (Or score) = some users have more than one score.

How can I get the number of score (Or users) using this mysql query?

SQL FIDDLE http://sqlfiddle.com/#!9/ff4505/4

  • 写回答

2条回答 默认 最新

  • dongpan2788 2016-01-18 08:55
    关注

    Check this ... I hope it help you ...

    SELECT user_id,@all_user := (SELECT COUNT(*) FROM `results`) as all_count,((@all_user-score)+1) as rank FROM `results` WHERE user_id = 3 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块