2012-12-10 215 views
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 

這是我的查詢時,我有2個或以上的團體在1個聯盟和球員得分各組中的目標,查詢返回的目標,但重複播放正確的數字,例如:MySQL查詢返回重複條目

John Doe got 3 goals in group 1 

John Doe got 4 goals in group 2 

查詢返回:

John Doe got 7 goals 

我的錯誤在哪裏?

回答

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 
+0

感謝您的回答向我指出正確的方向我只需要通過'id_player'補充條款'集團BY' – Viszman