php - Combine 3 SQL SELECT into one statement -


i have 3 tables :

  • teams (id_team, name, id_season)
  • seasons (id_season, name, nbr_teams)
  • teams_stats (id_stats, id_game, id_team, victory, defeat, draw)

i have 3 queries working fine individually :

select t.name, count(ts.victory) wins teams t join seasons s on t.id_season = s.id_season , s.name = '2015' left join teams_stats ts on ts.id_team = t.id_team , ts.victory = 1 group t.name  order t.name  select t.name, count(ts.defeat) losses teams t join seasons s on t.id_season = s.id_season , s.name = '2015' left join teams_stats ts on ts.id_team = t.id_team , ts.defeat = 1 group t.name  order t.name  select t.name, count(ts.victory) draws teams t join seasons s on t.id_season = s.id_season , s.name = '2015' left join teams_stats ts on ts.id_team = t.id_team , ts.draw = 1 group t.name  order t.name 

i wondering how can can same result within 1 query. can't ? maybe can throw light ...

i appreciate,

thanks.

you can use conditional aggregation:

select t.name,         sum(ts.victory=1) wins,        sum(ts.defeat=1) losses,        sum(ts.draw =1) draws teams t join seasons s on t.id_season = s.id_season , s.name = '2015' left join teams_stats ts on ts.id_team = t.id_team  group t.name  order t.name 

demo here


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -