Select Top 3 Scores Per Game

Discussion in 'PHP' started by dmm2020, Sep 15, 2009.

  1. #1
    Got a question, just to save time and accelerate a request people have on one of my mods. How would I write a good SQL to pick out the top 3 scores per game assuming that the structure is id, gameid, score, personid. I got it to work on group by but I want the top 3 to be picked out in the result array for each game. The high scores table may have hundreds of scores for any of the games. Table is highscores and database type is MySQL.
     
    dmm2020, Sep 15, 2009 IP
  2. pneulameiro

    pneulameiro Peon

    Messages:
    440
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    select * from game order by score; then in the result set you can just pickup the first 3 rows.
     
    pneulameiro, Sep 15, 2009 IP
  3. dicecities

    dicecities Well-Known Member

    Messages:
    944
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #3
    If you want result each game you can add in:
    select * from game where gameid = id order by score

    not sure if can do all the games at once without group feature
     
    dicecities, Sep 15, 2009 IP
  4. tenev

    tenev Active Member

    Messages:
    322
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #4
    I think we need some more details here. you need to select Top 3 gamers per game right?

    if so, then
    the top 3 highscored people from a specified game will be:
    $query = select * from highscores where gameid=1 order by score desc limit 3;
    //will return best 3 players for game 1.
    
    PHP:


    that will show the top 3 players of a game, starting with highest result.
     
    tenev, Sep 15, 2009 IP
  5. dmm2020

    dmm2020 Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Again... select from will be from highscores

    fields are primid, gameid,score,userid (not exact but pseudocode kind of speak)

    I know how to bring all the games up with no problem, such as the SQL you posted. What I get stuckon and did in my job all the time is complex SQL statements

    What you posted I could of done myself if I wanted. I am not interested in ONE game, but the top 3 scores for ALL the games.


    The SQL (and I know it's possible, just stuck) should call up only the top 3 high scores for each game. Now keep in mind that there might be 20 high scores recorded for a game, 1 the next, so on.
     
    dmm2020, Sep 16, 2009 IP