0

for a soccer league i have two tables:

  • "teams" is a table that lists all teams as unique entries ('id', 'team_name')
  • "matches" is a table that shows the match results: In the columns 'home' and 'away' i save each game result. 'home_team_id' and 'away_team_id' is the association to the teams-table.

This is how my query looks like:

SELECT Teams, Sum(P) as 'Matches', Sum(W) as 'win', Sum(D) as 'draw', Sum(L) as 'lost',
       SUM(Pts) as 'points'
  FROM (
        SELECT home Teams, 1 P,
                IF (home > away,1,0) W,
                IF (home = away,1,0) D,
                IF (home < away,1,0) L,
                CASE 
                    WHEN home > away THEN 3
                    WHEN home = away THEN 1 
                    ELSE 0
                END PTS
          FROM `matches`

        UNION ALL
        SELECT away Teams, 1,
                IF (home < away,1,0),
                IF (home = away,1,0),
                IF (home > away,1,0),
                CASE 
                    WHEN home < away THEN 3
                    WHEN home = away THEN 1
                    ELSE 0
                END
          FROM `matches`
        ) AS ERG
GROUP BY Teams
ORDER BY SUM(Pts) DESC 

Now i want the team names (teams.team_name) from the team-table. To achieve this i tried several join-statements with no luck.

It's obvious that the teams-table can contain teams who did not attend a match. These teams need to be displayed with zero-results.

Therefore I tried a LEFT JOIN:

SELECT team_name AS 'Teams'
  FROM `teams`
  LEFT JOIN matches ON ( teams.id = matches.home_team_id )

right after the ORDER-line in the end. I got an error message. I use MySQL 5.1.44, so nested selects shouldn't be a problem.

Any idea?

4

2 回答 2

1

听起来你想做这样的事情。我更新了您的子查询以包含home_team_idaway_team_id,然后您将JOINteams表格上返回名称。:

SELECT Teams, 
  Sum(P) as 'Matches',
  Sum(W) as 'win',
  Sum(D) as 'draw',
  Sum(L) as 'lost',
  SUM(Pts) as 'points',
  h.team_name as HomeTeam,
  a.team_name as AwayTeam
FROM
(
  SELECT home Teams,
    1 P,
    IF (home > away,1,0) W,
    IF (home = away,1,0) D,
    IF (home < away,1,0) L,
    CASE WHEN home > away THEN 3 WHEN home = away THEN 1 ELSE 0 END PTS,
    home_team_id,
    away_team_id
  FROM `matches`

  UNION ALL

  SELECT away Teams,
    1,
    IF (home < away,1,0),
    IF (home = away,1,0),
    IF (home > away,1,0),
    CASE WHEN home < away THEN 3 WHEN home = away THEN 1 ELSE 0 END,
    home_team_id,
    away_team_id
  FROM `matches`
) AS ERG
LEFT JOIN `teams` h
  on ERG.home_team_id = h.home_team_id
LEFT JOIN `teams` a
  on ERG.away_team_id = a.away_team_id
GROUP BY Teams, home_team_id, away_team_id
ORDER BY SUM(Pts) DESC 

根据您的评论编辑#1,听起来您想要这个(参见SQL Fiddle with Demo):

SELECT TeamId, 
  t.team_name,
  Teams, 
  Sum(P) as 'Matches', 
  Sum(W) as 'win', 
  Sum(D) as 'draw', 
  Sum(L) as 'lost',
  SUM(Pts) as 'points'
FROM 
(
  SELECT home_team_id TeamId, 
      home Teams, 1 P,
      IF (home > away,1,0) W,
      IF (home = away,1,0) D,
      IF (home < away,1,0) L,
      CASE 
          WHEN home > away THEN 3
          WHEN home = away THEN 1 
          ELSE 0
      END PTS
  FROM `matches`

  UNION ALL
  SELECT away_team_id TeamId,
       away Teams, 1,
       IF (home < away,1,0),
       IF (home = away,1,0),
       IF (home > away,1,0),
       CASE 
           WHEN home < away THEN 3
           WHEN home = away THEN 1
           ELSE 0
        END
   FROM `matches`
) AS ERG
LEFT JOIN `teams` t
  ON ERG.TeamId = t.id
GROUP BY Teams, TeamId
ORDER BY SUM(Pts) DESC 
于 2012-09-25T10:52:30.157 回答
1

您可以像这样使用外部联接:

SELECT a.team_name, Sum(ERG.P) as 'Matches', Sum(ERG.W) as 'win', Sum(ERG.D) as 'draw', Sum(ERG.L) as 'lost',
       SUM(Pts) as 'points'
  FROM 
    Teams a
    left outer join
    (
    SELECT home Teams, 1 P,
            IF (home > away,1,0) W,
            IF (home = away,1,0) D,
            IF (home < away,1,0) L,
            CASE 
                WHEN home > away THEN 3
                WHEN home = away THEN 1 
                ELSE 0
            END PTS
      FROM `matches`

    UNION ALL
    SELECT away Teams, 1,
            IF (home < away,1,0),
            IF (home = away,1,0),
            IF (home > away,1,0),
            CASE 
                WHEN home < away THEN 3
                WHEN home = away THEN 1
                ELSE 0
            END
      FROM `matches`
    ) AS ERG
    on a.team_name=ERG.Teams
GROUP BY a.team_name
ORDER BY SUM(Pts) DESC 

这应该可以很好地为您解决问题。

我还写了一个相当长的问题和答案,它解释了联合、连接、外连接和各种其他好东西——我在回答这样的问题时会链接到这些东西。它非常长而详细,并详细解释了所有步骤。

于 2012-09-25T10:52:45.950 回答