2016-05-31 31 views
0

我正在構建一個系統,該系統將顯示300個可以並且通常每天都會更改的MMO遊戲的最高排名玩家。所以這裏是我想要做的:具有雙限制選項的Selectin行

選擇前300名球員,對於這300名球員,我想從同一張表中選擇30個最近的參賽作品,其中我選擇當前排名前300的參賽作品。

這是我目前的查詢,它成功和準確地選擇了前300名球員。

SELECT * FROM `experiencehistory` WHERE `worldid` = `7` ORDER BY `experiencehistory`.`date` DESC, `experiencehistory`.`rank` ASC LIMIT 300 

這是我的數據庫結構:

ID - Identifies the entry for deletions etc later on. 
Characterid - Which character does this entry belong to. 
worldid - On which world does this character exist 
rank - what is the current (at the time of the entry, which is daily) rank the player has 
date - what date was the entry made on. (It saves a date through mktime() which is the day at 4am, all date entries for a specific date is the same. 

現在,使用上面的查詢我有球員之前已經指出,我怎樣才能使這個查詢還獲取他們的最後30個條目?我,我認爲它不會工作,只是簡單地循環,併爲每個字符做另一個查詢。這最終以10-15秒的頁面加載和使用ALOT資源,這顯然不適用於現場環境。

我正在考慮使用Group by,但我不知道如何選擇它們。感謝任何和所有的幫助。

+0

因此,您最多需要每位玩家30條記錄,最多9000條記錄?或者你想在主要字符行中以某種方式聚合它們? – cyberbit

+0

對我來說沒有任何意義,請您重新翻譯一下這句話:*我想從同一個表中選擇30個最近的條目,其中我選擇了當前排名前300位。* –

+0

Cyber​​bit表示,感謝您理解我。我不擅長英語,但你得到了我想要做的。所以是的,那將是9000個記錄。我將把它整合到每週和每月的變化中(該部分正在工作,所以我只需要弄清楚如何提取數據)。 – Hultin

回答

0

從你的帖子,我猜不透你自己是怎麼取從每個球員的30個條目,但你可以做到這一點運行一些代碼爲每個玩家:

$query = "SELECT * FROM `experiencehistory` WHERE `worldid` = 7 ORDER BY `experiencehistory`.`date` DESC, `experiencehistory`.`rank` ASC LIMIT 300" 

if ($link->multi_query($query)) { 
    do { 
     if ($result = $link->store_result()) { 
      while ($row = $result->fetch_row()) { 

      // do what you have to do to get the 30 entries here. 

      } 
      $result->free(); 
     } 
    } while ($link->next_result()); 
} 

$link是您的連接。 ..

希望它的作品!

+0

感謝編輯@Darren! –