如果我的表看起来像这样,我如何在 mysql 中获得所需的结果。
结果|年份
1 |2011
2 |2011
1 |2011
0 |2011
1 |2012
2 |2012
1 = 赢,2 = 输,0 = 平局
每年都可以有多个这样的值。不知道如何获得如下所示的预期结果。
year won lost draw totalPlayed
2011 2 1 1 3
2012 1 1 0 2
我尝试了以下查询,但没有得到想要的结果
select year,
league_types.league_name,
sum(if(result = 1,1,0)) as won,
sum(if(result = 0,1,0)) as draw,
sum(if(result = 4,1,0)) as noResult,
sum(if(result = 2,1,0)) as lost,
sum(if(result = 3,1,0)) as tied,
sum(if(result > 0 and result < 4,1,0)) as played
from match_score_card
inner join fixtures on match_score_card.match_id = fixtures.match_id
inner join league_types on fixtures.league_id = league_types.league_id
where
team_id = 1 group by year order by year desc