使用Union All和Join for League表进行Mysql查询

时间:2012-09-25 10:40:27

标签: mysql sql

对于足球联赛我有两张桌子:

  • “团队”是一个将所有团队列为唯一条目的表格('id', 'TEAM_NAME')
  • “matches”是一个显示匹配结果的表:在列中 'home'和'away'我保存每个游戏结果。 'home_team_id'和 'away_team_id'是与teams-table的关联。

这是我的查询的样子:

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 

现在我想要团队表中的团队名称(teams.team_name)。为了达到这个目的,我尝试了几个连接语句而没有运气。

很明显,球队表可以包含没有参加比赛的球队。这些团队需要显示零结果。

因此我尝试了LEFT JOIN:

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

在ORDER-line之后。我收到了一条错误消息。 我使用MySQL 5.1.44,因此嵌套选择应该不是问题。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

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 

答案 1 :(得分: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 

这应该很适合你。

我还写了一篇相当冗长的question and answer来解释工会,联接,外连接和各种其他好东西 - 我在回答这样的问题时链接到了这些东西。它非常详细,详细解释了所有步骤。