jaymc Posted January 11, 2009 Share Posted January 11, 2009 I have a table which contains game scores Each game is allowed 10 scores. There are 10 games. The scores table has 100 records.. I want to create a query that will pull out the top score for each game, heres what I tried SELECT game_id, username, MAX(score) score FROM arcade_scores The MAX(score) works as it pulls out the highest score but it does not pull the same username from that row. Thats where I am stuck Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/140354-solved-max/ Share on other sites More sharing options...
Mchl Posted January 11, 2009 Share Posted January 11, 2009 SELECT game_id, username, score FROM arcade_scores GROUP BY game_id HAVING MAX(score) edit No... apprentyl not... Quote Link to comment https://forums.phpfreaks.com/topic/140354-solved-max/#findComment-734467 Share on other sites More sharing options...
corbin Posted January 11, 2009 Share Posted January 11, 2009 SELECT game_id, username, MAX(score) score FROM arcade_scores GROUP BY game_id ORDER BY score DESC LIMIT 10; That might work, but I doubt it. Quote Link to comment https://forums.phpfreaks.com/topic/140354-solved-max/#findComment-734528 Share on other sites More sharing options...
fenway Posted January 11, 2009 Share Posted January 11, 2009 SELECT game_id, username, MAX(score) score FROM arcade_scores GROUP BY game_id ORDER BY score DESC LIMIT 10; That might work, but I doubt it. No, it won't work. This is the MOST common question asked on this forum. What you have to remember is that using "group by" means that mysql throws away every other column in the table -- and just tallies the results of the aggregate function you requested, like MAX, COUNT, etc. That means that you CANNOT get back any other columns at the same time... you'll need another query. The human brain does this in two steps as well, you just don't notice: you find the max value by looking at every row ONCE, and THEN you find the matching username by looking at every row AGAIN to find that one with the max value. So: SELECT t1.game_id, t1.score, t1.username FROM arcade_stores AS t1 INNER JOIN ( SELECT game_id, MAX(score) AS score FROM arcade_scores GROUP BY game_id ) AS t2 USING ( game_id, score ) Quote Link to comment https://forums.phpfreaks.com/topic/140354-solved-max/#findComment-734579 Share on other sites More sharing options...
corbin Posted January 11, 2009 Share Posted January 11, 2009 SELECT game_id, username, MAX(score) score FROM arcade_scores GROUP BY game_id ORDER BY score DESC LIMIT 10; That might work, but I doubt it. No, it won't work. This is the MOST common question asked on this forum. What you have to remember is that using "group by" means that mysql throws away every other column in the table -- and just tallies the results of the aggregate function you requested, like MAX, COUNT, etc. That means that you CANNOT get back any other columns at the same time... you'll need another query. The human brain does this in two steps as well, you just don't notice: you find the max value by looking at every row ONCE, and THEN you find the matching username by looking at every row AGAIN to find that one with the max value. So: SELECT t1.game_id, t1.score, t1.username FROM arcade_stores AS t1 INNER JOIN ( SELECT game_id, MAX(score) AS score FROM arcade_scores GROUP BY game_id ) AS t2 USING ( game_id, score ) Yeah, that's what I thought.... Today's been an off day.... lol. Quote Link to comment https://forums.phpfreaks.com/topic/140354-solved-max/#findComment-734587 Share on other sites More sharing options...
jaymc Posted January 11, 2009 Author Share Posted January 11, 2009 Once again fenway strikes Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/140354-solved-max/#findComment-734740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.