2012-11-01 77 views
0

我有一個看起來像這些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 
.... 

條目匹配兩個MySQL表

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中。

+0

您將需要加入相匹配的值在團隊中的值。我認爲主持人和客人是球隊表中的價值觀? –

+0

是的,你需要有一個變量可以將兩個表連接在一起。然後,您可以使用左連接將它們連接在一起,如果該團隊沒有任何數據,則團隊數據將爲空。 –

+0

如果團隊聯繫或者如果forfiet不爲null,您希望如何處理它? –

回答

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 
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 
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