dougong7850 2012-08-23 12:12
浏览 89
已采纳

MySQL高分 - 用户的个人排名:重复的条目导致错误的值

I'm currently creating a page in PHP that will display a user and their ranks in my site's highscores.

However, I have one field that is different from the rest and isn't functioning as intended.

Here is my table:

-- ----------------------------
--  Table structure for `playerstats`
-- ----------------------------
DROP TABLE IF EXISTS `playerstats`;
CREATE TABLE `playerstats` (
  `uid` int(11) NOT NULL DEFAULT '0',
  `gamelevel` int(11) NOT NULL DEFAULT '0',
  `overall` int(11) NOT NULL DEFAULT '0',
  `overallxp` bigint(20) NOT NULL DEFAULT '0',
  KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

...and some example data:

INSERT INTO `playerstats` VALUES ('14950', '123', '1495', '129825211')
INSERT INTO `playerstats` VALUES ('28967', '124', '1495', '127168799')
INSERT INTO `playerstats` VALUES ('95848', '121', '1495', '108481173')

Here is my query:

SELECT count(*) + 1 FROM (SELECT uid, overall, overallxp, gamelevel FROM playerstats GROUP BY playerstats.uid) AS x WHERE overall > (SELECT overall FROM playerstats WHERE uid = ". $userid .")"

...and $userid is:

$userid = (int) $_GET['searched'];

Now, when I navigate to the personal highscores of userid14950, it displays the correct overall ranking for that user because they are the person with the highest overallxp for their overall. However, when I visit the personal highscores of userid28967 or userid95848, their overall rank is the same as userid14950 for some reason (most likely because I don't account for users with the same overall result).

My question is: how would I go about making it so if two (or more) users share the same overall, they have their correct rank displayed, and not a duplicate one?

So that's about it.
Any help is very much appreciated :)

Thanks,
Mark

  • 写回答

1条回答 默认 最新

  • douxiong4250 2012-08-23 12:48
    关注

    try this:

    SET @rank = 0;
    SELECT rank FROM (
        SELECT @rank:=@rank + 1 AS rank, uid FROM playerstats ORDER BY overall DESC, 
        overallxp  DESC
    ) as tmp WHERE uid = 14950
    

    the avove query will return the rank for user14950

    This query will list all the users and their ranks

    SET @rank=0;
    SELECT rank, uid, overall, overallxp FROM (
    SELECT @rank:=@rank + 1 AS rank, uid, overall, overallxp FROM playerstats ORDER BY overall DESC, overallxp DESC
    ) as tmp 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题