php - generating sports league table from results table -


i have table containing teams in league's fixtures , results. attempting generate standings table results. thought had working upon manually calculating standings did not match table mysql output.

$query_away = " select teams.team_name,     sum(if(fixtures.away_team_score > fixtures.home_team_score,3,0)) w,     sum(if(fixtures.away_team_score = fixtures.home_team_score,1,0)) d,     sum(if(fixtures.away_team_score < fixtures.home_team_score,0,0)) l teams  inner join fixtures on teams.team_name = fixtures.home_team   group fixtures.home_team  order w desc"; 

it seams assigning unearned 3 pts teams did not win. there simpler way achieve or way fix code have? summarize, attempting count amount of times away team out scores home team , assign 3 pts that. 1 pt tying opponent on 0 losing.

fiddle http://sqlfiddle.com/#!9/85813/1

edit

this query repeated twice once home standings , once away standings. joining away query on away_team fixed unearned 3pts problem if achieve standings 1 query. code below.

$fulltable = []; $sortedtable = [];  $query_away = " select teams.team_name,     sum(if(fixtures.away_team_score > fixtures.home_team_score,3,0)) w,     sum(if(fixtures.away_team_score = fixtures.home_team_score,1,0)) d,     sum(if(fixtures.away_team_score < fixtures.home_team_score,0,0)) l  teams  inner join fixtures on teams.team_name = fixtures.away_team   group fixtures.away_team  order w desc";    $query_home = " select teams.team_name,      sum(if(fixtures.home_team_score > fixtures.away_team_score,3,0)) w,     sum(if(fixtures.home_team_score = fixtures.away_team_score,1,0)) d,     sum(if(fixtures.home_team_score < fixtures.away_team_score,0,0)) l  teams  inner join fixtures on teams.team_name = fixtures.home_team   group fixtures.home_team  order w desc";   $home_result = mysqli_query($dbc, $query_home); $away_result = mysqli_query($dbc, $query_away); echo'<table><tr><th>home table</th><th>w</th><th>d</th><th>l</th><th>pts</th></tr>'; if (!$home_result) {     echo 'no result'; } else {     //print_r(mysqli_fetch_array($result));      while ($row = mysqli_fetch_array($home_result)) {         $pts = $row['w'] + $row['d'];         echo "<tr><td>" . $row['team_name'] . "</td><td>" . $row['w'] / 3 . "</td><td>" . $row['d'] . "</td><td>" . $row['l'] . "</td><td>" . $pts . "</td><tr>";         $hometeam = $row['team_name'];     $fulltable["$hometeam"] = $pts; }     echo'</table>'; } 

this query should 3 points victories both home , away @ same time. 1 sum needed ties because not matter if team home or away. no sum needed losses, because you'd adding bunch of zeros anyway.

select teams.team_name,     sum(if(teams.team_name = fixtures.away_team          , fixtures.away_team_score > fixtures.home_team_score,3,0))      + sum(if(teams.team_name = fixtures.home_team          , fixtures.home_team_score > fixtures.away_team_score,3,0)) w,     sum(if(fixtures.away_team_score = fixtures.home_team_score,1,0)) d,     0 l teams inner join fixtures on teams.team_name = fixtures.home_team      or teams.team_name = fixtures.away_team group teams.team_name order w desc 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -