Jump to content

Selecting row with maximum value in field?


Cep

Recommended Posts

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 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
)
)
LIMIT 0 , 30;
[/code]
Link to comment
Share on other sites

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
Share on other sites

Hi Fenway

thanks 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.