MySQL GROUP BY - 获取该特定组中的项目

时间:2009-07-02 14:06:54

标签: php mysql

我正在使用MySQL GROUP BY函数,想知道是否有办法获取该组的项目数而无需再次查询?

$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};
    }
}

我非常感谢你的回答。提前谢谢。

2 个答案:

答案 0 :(得分:3)

您可以获得COUNT

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

答案 1 :(得分:0)

只需在SELECT列表中添加另一列:“count(player_id)AS elementCount”