dtpt75860 2014-01-19 16:55
浏览 22
已采纳

根据ORDER获取每一行的位置

I have a highscore table that has three columns, name, highScore, rep. Currently I loop through the results ORDERED by rep. I have an $i est within the loop and this displays as rank eg:

SELECT highScore, name, rep FROM  table1 ORDER BY rep DESC

while ($row = $result->fetch_assoc()) 
        {
                echo "<tr>";
                echo "<td>" . $i . "</td>"; // rank
                echo "<td>" . $row["name"] . "</td>"; // name
                echo "<td>" . $row["rep"] . "</td>"; // rep
                echo "<td>" . $row["highScore"] . "</td>"; // high score
                echo "</tr>";
                $i++;
            }
        }

Currently this works without fault. The problem is that i have now implemened a "sort by" button which changes the SQL to ORDER to highScore instead of rep. the only problem is that the Rank does not match now. Is there a way of getting and echoing the position of each row based on rep and then seslcting and ordering based on highScore?

  • 写回答

2条回答 默认 最新

  • ds355020 2014-01-19 17:00
    关注
    select * from 
    (
      SELECT highScore, name, rep, @rank := @rank + 1 as rank 
      FROM table1, (select @rank := 0) r
      ORDER BY rep DESC
    ) alias_name
    order by highscore desc
    

    rank contains your rank by rep and the table is ordered by highscore now.

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

报告相同问题?

悬赏问题

  • ¥15 vscode问题请教
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM