Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8585979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:11:54+00:00 2026-06-11T22:11:54+00:00

for a soccer league i have two tables: teams is a table that lists

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:11:56+00:00Added an answer on June 11, 2026 at 10:11 pm

    It sounds like you want to do something like this. I updated your sub-queries to include the home_team_id and the away_team_id, then you will JOIN on the teams table to return the name.:

    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 
    

    Edit #1 based on your comments, it sounds like you want this (See 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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of soccer matches for which I'd like to display forms.
I have a script that creates a soccer fixture. Each club can play just
I'm working on a soccer league management website using Django. Among others, I have
In a tableview I'm displaying local soccer matches from an NSMutableArray that are planned
I have a table like this: user_id hobbie1 hobbie2 hobbie3 1 ski soccer tv
Need suggestions on implementing associating single or many objects to an entity. All soccer
I have an application which scrapes soccer results from different sources on the web.
I have soccer results data in the following format (thousands of observations): Div date
I have created a fixture generator for football/ soccer games... for ($round = 0;
I'm trying to create a REST service which shows/adds/deletes/edits soccer data in a database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.