2013-01-11 42 views
0

將是邏輯什麼成績頁面上獲得:所有玩家(如#115)中的遊戲PHP MySQL的成績頁面

  • 當前用戶順序:MySQL的選擇對所有用戶的ORDER BY成績DESC和然後檢查當前用戶在哪裏?

  • 更好的兩個分數:獲取當前用戶,然後選擇限制2 WHERE得分>「$ currentuserscore」?

  • 減去兩個分數:獲取當前用戶,然後選擇限制2 WHERE得分<「$ currentuserscore」?

我覺得這是非常錯誤的,有沒有其他方式?

回答

1

對於選項1:

select count(1) as count from users where score>=$user_score

這應該給你的人的等級。如果您擔心與當前用戶的分數相同的多個人將>=更改爲>,則可以將結果加1。

選項2和3:在一個查詢

select * from users where score>$user_score order by score asc limit 2

select * from users where score<$user_score order by score desc limit 2

或者你可以得到所有5(前2,後2和當前用戶)一旦你從第一等級查詢:

$start = $user_rank - 2; 
$query = "select * from users order by score asc limit {$start},5"; 

或者,如果您不希望當前用戶或與當前用戶具有相同分數的任何人加入您可以做到這一點:

$start = $user_rank - 2; 
$query = "select * from users where score!=$user_score order by score asc limit {$start},4"; 
+0

謝謝老兄,這是一個很大的幫助 – housamz