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] Quote Link to comment 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? Quote Link to comment 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] Quote Link to comment 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. Quote Link to comment 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] Quote Link to comment 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. Quote Link to comment 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.