how to select and group mysql data based on the follwoing table

Posted by user1151680 on Stack Overflow See other posts from Stack Overflow or by user1151680
Published on 2013-11-09T20:59:07Z Indexed on 2013/11/09 21:54 UTC
Read the original article Hit count: 150

Filed under:
|
|
|

how can I achieve the desired result in mysql if my table looks like this.

result|year

1 |2011

2 |2011

1 |2011

0 |2011

1 |2012

2 |2012

1 = Won, 2 = lost, 0 = draw

Every year can have multiple values like this. Not sure how I can get the desired result like below.

year  won  lost  draw  totalPlayed
2011  2     1     1       3
2012  1     1     0       2

I have tried the following query but does not get the desired result

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

© Stack Overflow or respective owner

Related posts about mysql

Related posts about select