drzrdc1766788 2013-11-09 02:28
浏览 33
已采纳

如何使用MySQL Query和PHP分发排名

I have to distribute ranking to the 5000 students in an exam. Ranking is based on the score and the time taken (in seconds) to obtain that score.

For example is 5 students have same score, then taken will be the criteria to calculate their ranks otherwise score should be the criteria to calculate their ranks.

Following is my table tbRank

ID  StudID  Score Time Date        Rank
1   11      8     60   09-11-2013 
2   22      6     45   09-11-2013
3   33      4     76   09-11-2013
4   44      6     67   09-11-2013
5   55      8     35   09-11-2013 
6   66      8     35   08-11-2013
7   77      8     39   08-11-2013

Now rank column in above table should be updated as:

ID  StudID  Score Time Date        Rank
1   11      8     60   09-11-2013  2
2   22      6     45   09-11-2013  3
3   33      4     76   09-11-2013  5
4   44      6     67   09-11-2013  4
5   55      8     35   09-11-2013  1
6   66      8     35   08-11-2013  1
7   77      8     39   08-11-2013  2

I want to make a MySQL Query to do this business. Similarly there can be more than 10000 records in the table. So I need an optimized query for this functionality.

Note: I am using PHP and MYSQL.

Update: Everyday almost 5000 new entries will be created in the table and after all insertions are made, rank column will be updated once in a day. Now please suggest me the best way to do this. If I update rank column in the table, then only once I will have to do it, otherwise everytime while fetching the rank of the student, I will have to make calculations.

  • 写回答

2条回答 默认 最新

  • douchibu7040 2013-11-09 02:45
    关注

    If you want to update the table you can do:

    UPDATE tbRank t
    INNER JOIN (
      SELECT ID, StudID, Score, `Time`, @rownum := @rownum + 1 AS rank
      FROM (
        SELECT ID, StudID, Score, `Time`
        FROM tbRank
        WHERE date = date(now())
        ORDER BY score DESC, `time` ASC
        ) a
      JOIN (
        SELECT @rownum := 0
        ) r
      ) b ON b.id = t.id
    SET t.rank = b.rank
    

    But since you would do this every day, you would have to update all rows of rank column to null before executing this.

    I wouldn't advise this approach because if you change one of them or insert a new record, you have to do the update again.

    You can simply use this query whenever you want to get the ranking:

    SELECT ID, StudID, Score, `Time`, @rownum := @rownum + 1 AS rank
      FROM (
        SELECT ID, StudID, Score, `Time`
        FROM tbRank
        WHERE date = date(now())
        ORDER BY score DESC, `time` ASC
        ) a
      JOIN (
        SELECT @rownum := 0
        ) r
    

    Additionally, if you want to know the rank of a specific student, you can do the following query:

    SELECT * FROM 
    (
      SELECT ID, StudID, Score, `Time`, @rownum := @rownum + 1 AS rank
      FROM (
        SELECT ID, StudID, Score, `Time`
        FROM tbRank
        WHERE date = date(now())
        ORDER BY score DESC, `time` ASC
        ) a
      JOIN (
        SELECT @rownum := 0
        ) r
      ) b
    WHERE b.StudID = 11;
    

    <kbd>sqlfiddle demo</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改