dongshi2458 2018-09-03 01:44
浏览 57
已采纳

如何排序mysql数据并找到位置? [关闭]

I have mysql data of 3 rows (user_id, exam_id, marks). I want to find each user ranking position based on marks and exam_id . How will be the SQL query with php code for this. Please help me. Thanks in advance.

Here is partial code

$p = "SELECT * FROM IDTABLE WHERE EID = '$info[EID]'";
$rowp = mysqli_query($conn, $p);
while (($ret = mysqli_fetch_assoc($rowp)) > 0) {
    $q = "SELECT * FROM INFOTABLE WHERE EID = '$ret[EID]'";
    $rowq = mysqli_query($conn, $q);
    while (($retq = mysqli_fetch_assoc($rowq)) > 0) {                                
        $user[$ret['EID'] . $retq['UID']] = $retq['Marks'];                                
    }
}
arsort($user);
var_dump($user);
  • 写回答

1条回答 默认 最新

  • dti70601 2018-09-03 02:23
    关注

    First of all you should consider a few things:

    • You should always better search for an already answered question before posting a new one (it's part of stack overflow's code)
    • Bring more information of what you want, being as clear as possible
    • Giving more details (for example: you sql structure)

    However, I will try to give you a hint - Hope it helps!

    • For example you want to order your user_id based on their marks:

      select user_id from my_table order by "marks" asc;

    • For example you want to order your user_id based on their marks and exam_id :

      select user_id from my_table order by "marks", "exam_id" asc;

    • For example you want to create a kind of ranking based on users's marks:

      SET @rank := 0; SELECT *, @rank := @rank + 1 AS rank FROM my_table order by marks ASC;

    • For example you want to get the ranking number from a specific

      SET @rank := 0; SELECT * from ( select *, @rank := @rank + 1 FROM my_table order by marks ASC ) AS rank where user_id = 2;

    Hope this helps! You could get more information about mysql's ranking here

    Regards,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算