2011-12-13 33 views
0

所有在我的PHP的遊戲玩家都存儲在一個名爲players表,該表中有字段idnamescoreSQL PHP的記錄位置

假設我在一個玩家資料頁面,我想知道如何從所有遊戲玩家中獲得他的得分位置。所以如果他在整個表格中得分最高,他的位置就是#1。

我該如何編寫查詢?

+0

我會看看從這個SO問題接受的答案:[使用MySQL,如何選擇一個特定行的查詢結果排名?](http://stackoverflow.com/questions/1262448)/using-mysql-how-do-i-select-query-result-rank-of-one-particular-row) – rdlowrey

+0

你想如何處理與'score'並列的玩家? – pilcrow

回答

1

這個怎麼樣?

SELECT COUNT(id)+1 AS rank FROM players WHERE score>[yourPlayerScore] 
+0

簡單,完美 – user1022585

0

你能試試嗎? :

SET @rank=0; 
SELECT rank 
FROM (
    SELECT @rank:[email protected]+1 AS rank, name, score 
    FROM players 
    ORDER BY score DESC 
) ranks 
WHERE id = <id of person whose profile is showing>;