MySQL: Getting a Query out of the While Loop -
the following simple example of multi-player game many games played on several seasons. game id, player name, score of each player , season being played recorded in table. queries below show how 1 can derive summary of player's rank games played in particular season.
select game, score table name='name' , season='season' order game; while(fetching result) { select count(*) 'total', sum(case when score>='score' 1 else 0 end) 'rank' table game='game' , season='season'; }
the output contains game, score, rank , total games played player in particular season, order game
. wondering whether there more efficient ways instead of using while
loop.
sorry, got confused column name when first posted. cannot make of optimization here in query itself, because second time want data games, not player trying rank. can add them subqueries/joins first, second query works more 1 row, can done, result in additional rows being returned.
one way make things work faster when retrieving data add new information in db - rank each player's games , total participants in game. can calculate right after game finishes, same way second query, , save results somewhere can access them faster without calculating information each time need it.
Comments
Post a Comment