1
 SELECT *
 FROM ( SELECT a.*, a.id AS id_player, 
        (SELECT COUNT(id)
         FROM `vd7qw_footsal_goals`
         WHERE a.id = id_player
         AND id_group IN (SELECT id_group
                          from `vd7qw_footsal_groupofleague`
                          WHERE id_league = 2)
         ) AS goals, 
         team.team_name
         FROM `vd7qw_footsal_players` AS a
         LEFT JOIN vd7qw_footsal_teams AS team
              ON team.id = a.id_team
         LEFT JOIN vd7qw_footsal_teamofgroup AS tog
              ON tog.id_team = team.id
         LEFT JOIN vd7qw_footsal_groups AS g
              ON g.id = tog.id_group
         WHERE (a.state IN (1))
        ) AS h
 WHERE goals > 0
 ORDER BY goals DESC

这是我的查询,当我在 1 个联赛中有 2 个或更多组并且球员在每个组中进球时,查询返回正确的进球数但重复球员,例如:

John Doe got 3 goals in group 1

John Doe got 4 goals in group 2

查询返回:

John Doe got 7 goals

我的错误在哪里?

4

1 回答 1

2

尝试GROUP BY

SELECT *
 FROM ( SELECT a.*, a.id AS id_player, (SELECT COUNT(id)
 FROM `vd7qw_footsal_goals`
 WHERE a.id = id_player
 AND id_group IN (SELECT id_group
 from `vd7qw_footsal_groupofleague`
 WHERE id_league = 2)) AS goals, team.team_name
 FROM `vd7qw_footsal_players` AS a
 LEFT JOIN vd7qw_footsal_teams AS team
 ON team.id = a.id_team
 LEFT JOIN vd7qw_footsal_teamofgroup AS tog
 ON tog.id_team = team.id
 LEFT JOIN vd7qw_footsal_groups AS g
 ON g.id = tog.id_group
 WHERE (a.state IN (1))) AS h
 WHERE goals > 0
GROUP BY id_group
 ORDER BY goals DESC
于 2012-12-10T12:41:57.310 回答