tags:

views:

82

answers:

2

I am using the MySQL GROUP BY function, and want to know if there is a way to get the number of items for that group without having to a query again?

$homePointsPlayerResult = mysql_query("SELECT `player_id` FROM `conversions` WHERE `game_id` = '$game_id' AND `team_id` IS NULL GROUP BY `player_id`");

while ($players_with_points_conversions[] = mysql_fetch_row($homePointsPlayerResult)) {
    if (array_search($players_with_points_conversions['player_id'],$home_players,true) == FALSE) {
     $home_players['player_id'] = $players_with_points_conversions['player_id'];
     $home_players['conversions'] = {WANT NUMBER OF ELEMENTS FOR THIS GROUP};
    }
}

I would really appreciate your answers. Thanks in advance.

+3  A: 

You can get the COUNT:

SELECT `player_id`, COUNT(*) AS conversions FROM `conversions` WHERE `game_id` = '$game_id' AND `team_id` IS NULL GROUP BY `player_id`
Greg
A: 

Just add another column to the SELECT list along the lines of: "count(player_id) AS elementCount"

Jason Musgrove