Cep Posted January 23, 2007 Share Posted January 23, 2007 Hello,I have written a query which will count the results of card games. I am basically counting how many times a player has been beaten by all the other players for every game they have played. I have don't this using a field called count. I now have a record set but I only want the row or rows with the highest count value, is it possible to pick these out and leave the rest?Here is my query[code]SELECT cw_results.player_id, cw_results.outby_id, cw_players.name, Count( cw_players_1.name ) AS countFROM cw_players AS cw_players_1RIGHT JOIN ((cw_gamesRIGHT JOIN cw_results ON cw_games.game_id = cw_results.game_id)LEFT JOIN cw_players ON cw_results.outby_id = cw_players.player_id) ON cw_players_1.player_id = cw_results.player_idGROUP BY cw_results.player_id, cw_results.outby_id, cw_players.nameHAVING (((cw_results.player_id) =1)AND ((cw_results.outby_id) NOT LIKE 0))LIMIT 0 , 30;[/code] Link to comment https://forums.phpfreaks.com/topic/35381-selecting-row-with-maximum-value-in-field/ Share on other sites More sharing options...
fenway Posted January 23, 2007 Share Posted January 23, 2007 I'm having trouble parsing that myself... and right join? Link to comment https://forums.phpfreaks.com/topic/35381-selecting-row-with-maximum-value-in-field/#findComment-167467 Share on other sites More sharing options...
Cep Posted January 24, 2007 Author Share Posted January 24, 2007 I copied that from phpmyadmin after I had manually entered the sql and it ran ok. try this from my script,[code]SELECT cw_results.player_id, cw_results.outby_id, cw_players.name, Count(cw_players_1.name) AS count FROM cw_players AS cw_players_1 RIGHT JOIN ((cw_games RIGHT JOIN cw_results ON cw_games.game_id = cw_results.game_id) LEFT JOIN cw_players ON cw_results.outby_id = cw_players.player_id) ON cw_players_1.player_id = cw_results.player_id GROUP BY cw_results.player_id, cw_results.outby_id, cw_players.name HAVING (((cw_results.player_id)=1) AND ((cw_results.outby_id) Not Like 0))[/code] Link to comment https://forums.phpfreaks.com/topic/35381-selecting-row-with-maximum-value-in-field/#findComment-167927 Share on other sites More sharing options...
fenway Posted January 24, 2007 Share Posted January 24, 2007 That HAVING should probably be WHERE... and "not like 0" could probablybe ">0"... if you want just the highest count, you'll have to wrap the whole thing again, and select MAX() of that. Link to comment https://forums.phpfreaks.com/topic/35381-selecting-row-with-maximum-value-in-field/#findComment-167997 Share on other sites More sharing options...
Cep Posted January 24, 2007 Author Share Posted January 24, 2007 Hi Fenwaythanks for the reply, is this what you meant for the corrected SQL?[code]SELECT cw_results.player_id, cw_results.outby_id, cw_players.name, Count(cw_players_1.name) AS count FROM cw_players AS cw_players_1 RIGHT JOIN ((cw_games RIGHT JOIN cw_results ON cw_games.game_id = cw_results.game_id) LEFT JOIN cw_players ON cw_results.outby_id = cw_players.player_id) ON cw_players_1.player_id = cw_results.player_id GROUP BY cw_results.player_id, cw_results.outby_id, cw_players.name WHERE MAX((((cw_results.player_id)=1) AND ((cw_results.outby_id) > 0)))[/code][/code] Link to comment https://forums.phpfreaks.com/topic/35381-selecting-row-with-maximum-value-in-field/#findComment-168012 Share on other sites More sharing options...
fenway Posted January 24, 2007 Share Posted January 24, 2007 No, I mean that once you generate your counts, you'll need to use that derived table to find the max() for each one. Link to comment https://forums.phpfreaks.com/topic/35381-selecting-row-with-maximum-value-in-field/#findComment-168023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.