0

私はこれらのように見える2つのMySQLテーブルを持っています

チーム

id  name    abbr_name   badge   slogan  captain
1   TEAM1   TM1     NULL    NULL    NULL
2   TEAM2   TM2     NULL    NULL    NULL
3   TAEM3   TM3     NULL    NULL    NULL
4   TEAM4   TM4     NULL    NULL    NULL
5   TEAM5   TM5     NULL    NULL    NULL
....

一致エントリ

id  season  match_day   host    guest   time    host_score  guest_score  forfait mvp
1   1   1   3   4   NULL    10  2   NULL    43
2   1   1   5   6   NULL    2   2   NULL    16
3   1   1   9   10  NULL    6   3   NULL    49
4   1   1   2   1   NULL    5   5   NULL    29
5   1   1   7   8   NULL    4   2   NULL    52
6   1   2   1   4   NULL    7   3   NULL    75
....

私がやりたいのは、すべての行に値を与えることができるクエリを作成することです

Team_Id, Team_name, Team_N_Match_Played, Team_N_Match_Won, Team_N_Match_Lost

PHPですべてのデータを操作し、代わりにMySQLクエリを直接使用せずにそれは可能ですか?MySQLでそれをどのように実行できるかわかりません。

4

3 に答える 3

1

MySQL Countを使用します。

SELECT 
  t.Team_Id, t.Team_name, 
  COUNT(meh.id) + COUNT(meg.id) AS Team_N_Match_Played, 
  COUNT(meh.host_scrore > meh.guest_score) + COUNT(meg.guest_score > meg.host_scrore ) AS Team_N_Match_Won, 
  Team_N_Match_Lost 
COUNT(meh.host_scrore < meh.guest_score) + COUNT(meg.guest_score < meg.host_scrore )
FROM Teams AS t
JOIN Match_Entries AS meh ON t.id = meh.host
JOIN Match_Entries AS meg ON t.id = meg.guest
于 2012-11-01T13:05:16.243 に答える
0

このようなものはそれを行うでしょう

SELECT Teams.id, Teams.name, COUNT(a.id) + COUNT(d.id) + COUNT(c.id) + COUNT(f.id) + COUNT(b.id) + COUNT(e.id) AS GamesCnt, COUNT(a.id) + COUNT(d.id) AS GamesWon, COUNT(c.id) + COUNT(f.id) AS GamesLost, COUNT(b.id) + COUNT(e.id) AS GamesDrawn
FROM Teams
LEFT OUTER JOIN MatchEntries a ON Teams.id = a.host AND a.host_score > a.guest_score
LEFT OUTER JOIN MatchEntries b ON Teams.id = b.host AND b.host_score = b.guest_score
LEFT OUTER JOIN MatchEntries c ON Teams.id = c.host AND c.host_score < c.guest_score
LEFT OUTER JOIN MatchEntries d ON Teams.id = d.guest AND d.guest_score > d.host_score
LEFT OUTER JOIN MatchEntries e ON Teams.id = e.guest AND e.guest_score = e.host_score
LEFT OUTER JOIN MatchEntries f ON Teams.id = f.guest AND f.guest_score < f.host_score
GROUP BY Teams.id
于 2012-11-01T14:29:47.930 に答える
0

MatchEntries = dc_match チーム = dc_teams

SELECT
    team_id,
    name,
    SUM(P) AS played,
    SUM(W) AS won,
    SUM(D) AS draw,
    SUM(L) AS lost,
    SUM(PTS) AS points
FROM(
    SELECT
        host team_id,
        CASE
            WHEN host_score IS NULL THEN 0
            ELSE 1
        END P,
        IF (host_score > guest_score,1,0) W,
        IF (host_score = guest_score,1,0) D,
        IF (host_score < guest_score,1,0) L,
        CASE 
            WHEN host_score > guest_score THEN 3
            WHEN host_score = guest_score THEN 1 
            ELSE 0
        END PTS
    FROM dc_match

    UNION ALL

    SELECT
        guest team_id,
        CASE
            WHEN guest_score IS NULL THEN 0
            ELSE 1
        END P,
        IF (guest_score > host_score,1,0) W,
        IF (guest_score = host_score,1,0) D,
        IF (guest_score < host_score,1,0) L,
        CASE 
            WHEN guest_score > host_score THEN 3
            WHEN guest_score = host_score THEN 1 
            ELSE 0
        END PTS
    FROM dc_match
    ) AS RNK
LEFT JOIN dc_team t
    ON RNK.team_id = t.id
GROUP by team_id
ORDER BY points DESC
于 2012-11-01T14:47:55.323 に答える