CREATE TABLE `players` (
`pid` int(2) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`score` int(2) NOT NULL,
`game` varchar(20) NOT NULL,
PRIMARY KEY (`pid`),
UNIQUE KEY `pid` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `players` (`pid`, `name`, `score`, `game`) VALUES
(1, 'Samual', 25, 'aa'),
(2, 'Vino', 20, 'aa'),
(3, 'John', 20, 'aa'),
(4, 'Samual', 22, 'bb'),
(5, 'Samual', 21, 'aa'),
(6, 'Vino', 24, 'aa'),
(7, 'John', 25, 'aa'),
(8, 'Vino', 26, 'cc'),
(9, 'John', 23, 'cc'),
(10, 'John', 19, 'aa'),
(11, 'Vino', 20, 'aa'),
(12, 'Samual', 20, 'aa');
的總和在上表中,我想這個查詢到的分數由玩家所獲得的總和降序排列基地取一個球員的排名在比賽中進行的特定比賽。取得PHP MYSQL排名查詢排名基於得分
SELECT pid, name, SUM(score) as score, game, rank
FROM (
SELECT pid, name, score, game,
@curRank := IF(@prevRank = score, @curRank, @incRank) AS rank,
@incRank := @incRank + 1,
@prevRank := score
FROM player p, (SELECT @curRank :=0, @prevRank := NULL, @incRank := 1) r
WHERE game='aa'
ORDER BY score DESC
) s WHERE name ='John'
但查詢不輸出結果在一個所謂的格式,我想查詢總結了所有的球員得分在特定的遊戲,並給予該玩家的等級並考慮領帶情況
謝謝。
讀者回答這個問題可能首先要檢查的OP的排名SQL問題:[一](HTTP: (http://stackoverflow.com/q/29388723),[two](http://stackoverflow.com/q/29391651),[three](http://stackoverflow.com/q/29451275),[four](http ://stackoverflow.com/q/29452337)和[五](http://stackoverflow.com/q/29486804),以便工作不會意外重複。 – halfer 2015-04-07 08:48:47