I know this been asked many times here, been through most of the posts here, but I can’t get it working so thought to ask it again.
The problem:
There’s a score list, I’m trying to show user what’s his/her position in the score list. There’s a player_id, user is selected from two tables(players.id, scores.player_id). I’ve never tried such query, I wrote sth like this based on the instruction given in this site but no luck, this query isn’t right, kinda lost here.
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
Result:
Unknown column 'scores.score' in 'field list
thanks!
Score table Structure:
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;
—
— Dumping data for table 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');
Player table Structure:
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;
—
— Dumping data for table 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;
}
What if you change the table reference from scores to t1, since you’re using an alias to the subquery?
— edit
You can do a separate query returning the number of users playing and display ie. 25/100