MySQL查找行索引位置,复杂查询

时间:2012-12-24 11:01:49

标签: mysql select indexing position row

我知道这里曾经多次被问过,这里的大部分帖子都是这样,但是我无法让它工作,所以想再问一遍。

问题:
有一个分数列表,我试图向用户显示他/她在分数列表中的位置。有一个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;  
}

1 个答案:

答案 0 :(得分:2)

如果您将表引用从分数更改为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