idire Posted July 27, 2008 Share Posted July 27, 2008 What I need to do is find the maximum value of a column for each distinct username My previous query where all I was doing was looking for the highest values was: SELECT * FROM `record` WHERE skill = '$skill' ORDER BY xp_gained DESC LIMIT 0 , 10 I was trying something like this for only one record per person: SELECT DISTINCT (username), xp_gained, end_lvl, date FROM `record` WHERE skill = '$skill' ORDER BY xp_gained DESC LIMIT 0 , 10 This doesnt work, how would I retrieve the highest record for each user (but only 1 per user) Link to comment https://forums.phpfreaks.com/topic/116792-solved-php-distinct-with-multiple-rowscolumns/ Share on other sites More sharing options...
DarkWater Posted July 27, 2008 Share Posted July 27, 2008 Check out GROUP BY. Link to comment https://forums.phpfreaks.com/topic/116792-solved-php-distinct-with-multiple-rowscolumns/#findComment-600608 Share on other sites More sharing options...
idire Posted July 27, 2008 Author Share Posted July 27, 2008 I've read 8 pages on google on distinct / group by and cant make it work I tried: SELECT * FROM record WHERE skill = '$skill' GROUP BY username ORDER BY xp_gained DESC LIMIT 0, 10 doesnt give me the highest value of $skill for each user Link to comment https://forums.phpfreaks.com/topic/116792-solved-php-distinct-with-multiple-rowscolumns/#findComment-600609 Share on other sites More sharing options...
idire Posted July 27, 2008 Author Share Posted July 27, 2008 oh looks like this works: SELECT max(xp_gained), username, end_level, date FROM record WHERE skill = '$skill' GROUP BY username ORDER BY xp_gained DESC LIMIT 0, 10 Thanks Link to comment https://forums.phpfreaks.com/topic/116792-solved-php-distinct-with-multiple-rowscolumns/#findComment-600616 Share on other sites More sharing options...
idire Posted July 27, 2008 Author Share Posted July 27, 2008 correction it doesnt work it retrieves the results, but doesnt put them in order of highest xp_gained EDIT fixed it again using: SELECT username, max(xp_gained) AS xp_gained, end_level, date FROM record WHERE skill = '$skill' GROUP BY username ORDER BY xp_gained DESC LIMIT 0, 10 Link to comment https://forums.phpfreaks.com/topic/116792-solved-php-distinct-with-multiple-rowscolumns/#findComment-600620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.