【问题标题】:MySQL find row index position, complex queryMySQL查找行索引位置,复杂查询
【发布时间】:2012-12-24 11:01:49
【问题描述】:

我知道这个问题在这里被问过很多次,看过这里的大部分帖子,但我无法让它工作,所以想再问一次。

问题:
有一个分数列表,我试图向用户展示他/她在分数列表中的位置。有一个 player_id,用户是从两个表(players.id,scores.player_id)中选择的。我从来没有尝试过这样的查询,我根据这个网站给出的说明写了这样的东西但是没有运气,这个查询不对,有点迷失在这里。

SELECT 
    @rn:=@rn + 1 AS rank, scores.score
FROM
    (SELECT 
        scores.score, COUNT(*) AS ordercount
    FROM
        scores, players
    WHERE
        players.uid = '$uid'
            AND scores.version = '$version'
            AND scores.lvl = '$lvl'
            AND scores.player_id = players.id
    GROUP BY scores.score
    ORDER BY scores.score DESC) t1,
    (SELECT @rn:=0) t2

结果:

Unknown column 'scores.score' in 'field list

谢谢!

成绩表结构:

CREATE TABLE IF NOT EXISTS `scores` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `player_id` int(11) NOT NULL,
  `lvl` tinyint(4) NOT NULL,
  `score` bigint(20) NOT NULL,
  `played_times` bigint(20) NOT NULL,
  `version` varchar(10) NOT NULL,
  `ip` varchar(30) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

--

-- 转储表scores的数据

INSERT INTO `scores` (`id`, `player_id`, `lvl`, `score`, `played_times`, `version`, `ip`, `timestamp`) VALUES
(1, 1, 1, 9990, 4, '1', 'xx.xx.xx.xxx', '2012-12-24 01:46:10'),
(2, 1, 2, 5750, 1, '1', 'xx.xx.xx.xxx', '2012-12-24 01:46:49'),
(3, 1, 1, 10290, 5, '1', 'xx.xx.xx.xxx', '2012-12-24 02:47:25'),
(6, 1, 1, 13620, 6, '1', 'xx.xx.xx.xxx', '2012-12-24 10:40:26'),
(7, 2, 2, 241251, 2, '1', 'xx.xx.xx.xxx', '2012-12-24 19:03:22');

玩家表结构:

CREATE TABLE IF NOT EXISTS `players` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `email` varchar(255) NOT NULL,
  `pass` varchar(255) NOT NULL,
  `hash` varchar(255) NOT NULL,
  `uid` varchar(255) NOT NULL,
  `ip` varchar(30) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

--

-- 转储表scores的数据

INSERT INTO `players` (`id`, `name`, `email`, `pass`, `hash`, `uid`, `ip`, `timestamp`) VALUES
(1, 'Test', 'TEST@TEST.COM', '', '51516h0c0', '7b3bd627c9b0', 'xx.xx.xx.xxx', '2012-12-24 00:56:38');

PHP;

/*
    get player's topscore position among others
*/
function _getPlayerTopScorePos($uid,$lvl,$version)
{
    $uid            = _jClean($uid);
    $version        = _jClean($version);
    $lvl            = _jClean($lvl);

    include 'db.inc.php';

    $q = mysql_query("SELECT 
        @rn:=@rn + 1 AS rank, t1.score
    FROM
        (SELECT 
            scores.score, COUNT(*) AS ordercount
        FROM
            scores, players
        WHERE
                players.uid = '$uid'
                AND scores.version = '$version'
                AND scores.lvl = '$lvl'
                AND scores.player_id = players.id
        GROUP BY scores.score
        ORDER BY scores.score DESC) t1,
        (SELECT @rn:=0) t2");

    echo mysql_error();

    $x = mysql_fetch_array($q);
    $a = $x["rank"];

    @mysql_close();

    return $a;  
}

【问题讨论】:

    标签: mysql select indexing position row


    【解决方案1】:

    如果您将表引用从分数更改为 t1,因为您使用的是子查询的别名,该怎么办?

    SELECT 
            @rn:=@rn + 1 AS rank, t1.score
        FROM
            (SELECT 
                scores.score, COUNT(*) AS ordercount
            FROM
                scores, players
            WHERE
                players.uid = '$uid'
                    AND scores.version = '$version'
                    AND scores.lvl = '$lvl'
                    AND scores.player_id = players.id
            GROUP BY scores.score
            ORDER BY scores.score DESC) t1,
            (SELECT @rn:=0) t2
    

    --编辑

    SELECT COUNT(t1.player_id)
        FROM
            (SELECT 
                scores.player_id
            FROM
                scores
                INNER JOIN players ON scores.player_id = players.id
            WHERE
                players.uid = '$uid'
                AND scores.version = '$version'
                AND scores.lvl = '$lvl'
                AND scores.player_id = players.id
                AND scores.score >= '$score'
            GROUP BY scores.player_id) t1 
    

    您可以执行单独的查询,返回播放和显示的用户数,即。 25/100

    【讨论】:

    • 嗨 Edga,感谢您的回复。我已经测试了您的查询,似乎它应该可以工作,但我已经通过在分数列表中输入另一个用户 ID 的更好分数进行测试,但仍然返回排名为 1,它假设为 2。这是不对的,因为另一个用户在列表中的得分更高,因此它应该显示为第 2 而不是第 1。不管什么产生结果(=rank) 1. $x = mysql_fetch_array($q); $a = $x["排名"];回声$a;结果:1
    • 您最初的问题是否有错误?这听起来像是不同的问题,对吧?
    • 现在没有错误了,但是没有从分数列表中返回用户的位置。 :\
    • 您能否包括(编辑您的初始问题或打开一个新问题)数据样本、表结构以及@rn 的声明位置?
    • 我在主 Q 中添加了示例数据和 php func。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2018-07-07
    相关资源
    最近更新 更多