2011-11-04 13 views
12

Próbowałem znaleźć coś na ten temat i nie mogę niczego znaleźć, było kilka pytań tutaj, ale nie działały one dla mojego konkretnego projektu.jak wyprowadzić tabelę wyników w locie z tabeli mysql wyników piłki nożnej?

Zadałem podobne pytanie dotyczące aktualizacji tabeli, ale nie będzie działać na to, co naprawdę chcę tutaj jest lista wyników.

-------------------------------------------------------- 
|id | hometeam |goalsfor|goalsagainst| awayteam | 
-------------------------------------------------------- 
| 1 |Inter Milan | 3 |  1  | FC Barcelona | 
-------------------------------------------------------- 
| 2 |FC Barcelona | 1 |  0  | Inter Milan | 
-------------------------------------------------------- 
| 3 |Inter Milan | 4 |  0  | AC Milan  | 
-------------------------------------------------------- 
| 4 |AC Milan  | 0 |  2  | Inter Milan | 
-------------------------------------------------------- 
| 5 |Real Madrid | 2 |  0  | AC Milan  | 
-------------------------------------------------------- 
| 6 |AC Milan  | 2 |  2  | Real Madrid | 
-------------------------------------------------------- 
| 7 |FC Barcelona | 2 |  2  | AC Milan  | 
-------------------------------------------------------- 
| 8 |Real Madrid | 2 |  0  | Inter Milan | 
-------------------------------------------------------- 
| 9 |Inter Milan | 3 |  1  | Real Madrid | 
-------------------------------------------------------- 
| 10 |FC Barcelona | 2 |  0  | Real Madrid | 
-------------------------------------------------------- 
| 11 |Real Madrid | 1 |  1  | FC Barcelona | 
-------------------------------------------------------- 

Zasadniczo chcę móc utworzyć tabelę klasyfikacji rankingu drużyn w porządku, chcę przedstawić tę tabelę w locie i nie umieścić go w bazie

Pos Team   Pld W D L F A GD Pts 
1 FC Barcelona 5 2 3 0 8 5 3 9 
2 Inter Milan  6 2 2 2 11 10 1 8 
3 Real Madrid  6 2 2 2 8 8 0 8 
4 AC Milan  5 0 3 2 8 12 -4 3 

POS = pozycja W = wygrana D = remis L = strata F = zdobyte bramki za A = gole zdobyte przeciwko GD = różnice bramek Pts = punkty

Myślę, że najbardziej skutecznym sposobem na to byłoby przypisanie wygranych, remisów i strat, sumy zdobyte bramki i strzelone gole oraz kiedy odbijają się echem dane - obliczyć całkowitą wartość nu szum gier i punktów.

Ale w jaki sposób przypisać wygrane remisy lub straty? I obliczyć zdobyte bramki i gole?

+1

Czy możesz umieścić nagłówki kolumn na dolnym stole, abyśmy wiedzieli, jak obliczać każdą kolumnę? – nickb

+1

Czy masz inne nazwy zespołów z tabeli, czy też jest to jedyny sposób, aby je wszystkie, wybierając odrębną listę z Hometeam i Kasa? – megaflop

+0

@Wazzzy: D Są to rzeczywiste wyniki z losowych gier - tylko dla celów przykładowych, będę miał wiele wyników - może 10-15 lig przez ostatnie 60 - 70 lat - może posiadanie ich wszystkich w jednym stole mysql może być zły pomysł, ale pomyślę o tym później. Ale mogę dodać dodatkowe klauzule, wybierając konkursy zgodnie z danymi, których chce użytkownik. –

Odpowiedz

12

Pierwszy związek tabeli wyników razem zamieniając hometeam z dala i zamieniając liczbę bramek. To daje pewne dane źródłowe, które jest łatwo agregacji i zapytania do generowania karta wynik jest mniej więcej tak:

select 
    team, 
    count(*) played, 
    count(case when goalsfor > goalsagainst then 1 end) wins, 
    count(case when goalsagainst> goalsfor then 1 end) lost, 
    count(case when goalsfor = goalsagainst then 1 end) draws, 
    sum(goalsfor) goalsfor, 
    sum(goalsagainst) goalsagainst, 
    sum(goalsfor) - sum(goalsagainst) goal_diff, 
    sum(
      case when goalsfor > goalsagainst then 3 else 0 end 
     + case when goalsfor = goalsagainst then 1 else 0 end 
    ) score 
from (
    select hometeam team, goalsfor, goalsagainst from scores 
    union all 
    select awayteam, goalsagainst, goalsfor from scores 
) a 
group by team 
order by score desc, goal_diff desc; 
+2

fajnie i elegancko :) Wydaje się, że działa – roselan

+0

, ponieważ nie zwraca żadnych błędów - ale tylko pracuje nad tym, jak echo/wydrukować dane - nie spał wystarczająco dużo, więc wszystko jest trochę mgliste: S –

+0

to działa! ty jesteś geniuszem! –

3
// connection stuff 
$sql = 'select * from matchesTable'; 
$result = mysql_query($sql) 

$standings = array(); 
$standingTemplate = array ('matches' => 0, 'wins' => 0, 'draws' => 0, 'losses' => 0, 'goalsfor' => 0, 'goalsagainst' => 0, 'goalsdiff' => 0, 'points' => 0); 

while ($row = mysql_fetch_assoc($result)) 
{ 
    handleMatch($row['hometeam'], $row['goalsfor'], $row['goalsagainst']); 
    handleMatch($row['awayteam'], $row['goalsfor'], $row['goalsagainst']); 

    print_r(usort(standings, 'comparePoints')); // up to you to format the output as you like 
} 

function handleMatch($team, $goalsfor, $goalsagainst) 
{ 
    global $standings, $standingTemplate; 
    if ($goalsfor > $goalsagainst) 
    { 
     $points = 3; 
     $win = 1; 
     $draw = 0; 
     $loss = 0; 
    } 
    elsif ($goalsfor == $goalsagainst) 
    { 
     $points = 1; 
     $win = 0; 
     $draw = 1; 
     $loss = 0; 
    } 
    else 
    { 
     $points = 0 
     $win = 0; 
     $draw = 0; 
     $loss = 1; 
    } 

    if (empty($standings[$team]))$standing = $standingTemplate; 
    else $standing = $standings[$team]; 

    $standing['matches']++; 
    $standing['wins'] += $win; 
    $standing['draws'] += $draw; 
    $standing['losses'] += $loss; 
    $standing['goalsfor'] += $goalsfor; 
    $standing['goalsagainst'] += $goalsagainst; 
    $standing['goalsdiff'] += $goalsfor - $goalsagainst; 
    $standing['points'] += $points; 

    $standings[$team] = $standing; 

} 

function comparePoints($a, $b) 
{ 
    if ($a['points'] == $b['points']) 
    { 
     if ($a['goalsdiff'] == $b['goalsdiff']) return 0; 
     return ($a['goalsdiff'] < $b['goalsdiff']) ? 1 : -1 ; 
    }  
    return ($a['points'] < $b['points']) ? 1 : -1 ; 
} 

UWAGI: Nie testowałem go i wszystko, może być trochę błędów (brakuje niektórych $ lub ;).

+0

Niektóre małe błędy, jak powiedziałeś, ale łatwe do naprawienia i kod działa ... fajnie .. thnx. – fjckls

Powiązane problemy