2015-04-04 106 views
0
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' 

但查詢不輸出結果在一個所謂的格式,我想查詢總結了所有的球員得分在特定的遊戲,並給予該玩家的等級並考慮領帶情況

謝謝。

+0

讀者回答這個問題可能首先要檢查的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

回答

0

您希望使用group by語句而不是子查詢來解決此特定類型的問題。

+0

請@DaOgre幫助我解決此問題的正確查詢,並且我希望查詢考慮關係。謝謝 – Amacyber 2015-04-05 06:28:40

1

你需要做的子查詢聚集,然後使用這些變量來獲得排名:

select pid, name, game, score, (@rn := @rn + 1) as rank 
from (select pid, name, game, SUM(score) as score 
     from player 
     where game = 'aa' 
     group by pid, game 
    ) p cross join 
    (select @rn := 0) vars 
order by score desc; 
+0

謝謝@Gordon Linoff,但查詢仍然沒有給我我想要的 – Amacyber 2015-04-05 06:29:25

+0

@Amacyber:而不是問關於該主題的第五個問題,你可以在問題**中更具體的**關於你想要什麼? – halfer 2015-04-07 08:44:36