开发者

SQL Query GROUP BY?

Some f开发者_如何学Goriends and I are writing a php site where we can log our matches, create tournaments and log matches for these tournaments etc.

We hit a wall with displaying our matches for a certain tournament. We tried doing it with php but that messes up the tournament table totally

We have a table called matches where we have following columns:

ID  player1ID player2Id  team1ID team2ID ScorePlayer1 ScorePlayer2  Date TournamentID

Now we want to make tables like:

GPl        W    L    D    +    -    +/-       Pnts
Nielsen    6    1    3    2    3    6    -3    13
Chibax     6    2    1    3    4    3     1    9
Verre      6    1    3    2    3    6    -3    5
Hermie     6    4    1    1    7    2     5    5

But with php the rows with W L D and points never match , the whole table is weird.

Anyone have an idea of how we can do this with an SQL command ? (for non-soccer people: wins x 3 pnts Draws x 1 pnts losses 0 pnts


I don't know if mysql with php still shuns subqueries but the following might have a chance of working. I'll test it later on as I don't have the time at the moment. I just though I'd give you some ideas.

If the subqueries do not work then you could create a view of the subquery and proceed from there.

I'm also not sure if mysql supports column names that are expressed using double quotes ("+/-")

The basic idea is to select the score separately for home games and visiting games. Aggregate functions allow usually allow case statements within them that allows you to do a bit magic.

SELECT 
  player, 
  SUM(M) M, 
  SUM(W) W,
  SUM(E) E,
  SUM(D) D,
  SUM("+") "+",
  SUM("-") "-",
  SUM("+/-") "+/-",
  SUM(Pnts) Pnts
FROM (
  SELECT  
    player1ID player,
    count(*) M,
    SUM(case when ScorePlayer1 > ScorePlayer2 then 1 end) W,
    SUM(case when ScorePLayer1 < ScorePlayer2 then 1 end) E,
    SUM(case when ScorePlayer1 = ScorePlayer2 then 1 end) D
    SUM(ScorePlayer1) "+",
    SUM(ScorePlayer2) "-",
    SUM(ScorePlayer1) - SUM(ScorePlayer2) "+/-",
    SUM(case when ScorePlayer1 > ScorePlayer2 then 1 end) * 3 +
    SUM(case when ScorePLayer1 < ScorePlayer2 then 1 end) Pnts
  FROM 
    someTable
  GROUP BY
    player1ID
  UNION ALL
  SELECT  
    player2ID player,
    count(*) M,
    SUM(case when ScorePlayer2 > ScorePlayer1 then 1 end) W,
    SUM(case when ScorePLayer2 < ScorePlayer1 then 1 end) E,
    SUM(case when ScorePlayer2 = ScorePlayer1 then 1 end) D
    SUM(ScorePlayer2) "+",
    SUM(ScorePlayer1) "-",
    SUM(ScorePlayer2) - SUM(ScorePlayer1) "+/-",
    SUM(case when ScorePlayer2 > ScorePlayer1 then 1 end) * 3 +
    SUM(case when ScorePLayer2 < ScorePlayer1 then 1 end) Pnts
  FROM 
    someTable
  GROUP BY
    player2Id
 ) scoresheet
 ORDER BY
    Pnts desc,
    M asc,
    "+/-" desc,
    "+" desc,
    "-" asc

Edit: I forgot to order the results. You need a bit magic if you want to use matches between players A and B to order the results in case of a tie.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜