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 905821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:17:04+00:00 2026-05-15T16:17:04+00:00

I have a many to many table setup in my mysql database. Teams can

  • 0

I have a many to many table setup in my mysql database. Teams can be in many games and each game has 2 teams. There is a table in between them called teams_games.

What I am looking to do is create stats for each team. An ideal printout would be:

team_id, team_name, wins, losses, draws, error

My problem is linking the math of which team is the home or away and if they won, then counting those up. I would then have to sum those with the count of times each team was away and won. Then finally join everything together. My current query structure(which doeasn’t work correctly) is below along with the Create Table information. Any thoughts?


SELECT t.*, COUNT(g_wins.home_score > g_wins.away_score) AS wins,
COUNT(g_wins.home_score < g_wins.away_score) AS losses
FROM teams as t
JOIN teams_games AS t_g_wins ON t_g_wins.tid = t.tid
JOIN games AS g_wins on t_g_wins.gid = g_wins.gid"


Table Create Table

 teams  CREATE TABLE `teams` (
 `tid` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(60) NOT NULL,
`league` varchar(2) NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`tid`)

)


CREATE TABLE teams_games (

`tid` int(10) unsigned NOT NULL,
`gid` int(10) unsigned NOT NULL,
`homeoraway` tinyint(1) NOT NULL,
PRIMARY KEY (`tid`,`gid`),
KEY `gid` (`gid`),
CONSTRAINT `teams_games_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `teams` (`tid`),
CONSTRAINT `teams_games_ibfk_2` FOREIGN KEY (`gid`) REFERENCES `games` (`gid`)

)


CREATE TABLE games (

`gid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(60) NOT NULL,
`time` datetime NOT NULL,
`description` varchar(400) NOT NULL,
`error` smallint(2) NOT NULL,
`home_score` smallint(2) DEFAULT NULL,
`away_score` smallint(2) DEFAULT NULL,
PRIMARY KEY (`gid`)

)

  • 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-05-15T16:17:05+00:00Added an answer on May 15, 2026 at 4:17 pm

    You are complicating things: this should be a 2-n not n-m relationship.
    Get rid of teams_games table and make 2 fields to games table, ex: home_tid and away_tid

    EDIT: and the query would be then something similar:

    select t.tid, t.name, 
           sum(g.home_score < g.away_score xor t.tid = g.home_tid) wins, 
           sum(g.home_score > g.away_score xor t.tid = g.home_tid) losses, 
           sum(g.home_score = g.away_score) draws
      from games g
           join teams t on t.tid = g.home_tid or t.tid = g.away_tid
     group by t.tid
    

    so the answer is to use sum, otherwise it counts both true and false values in output which is total rows resulting from joins.

    EDIT2:

    select t.tid, t.name, 
           sum(g.home_score < g.away_score xor tg.homeoraway) wins, 
           sum(g.home_score > g.away_score xor tg.homeoraway) losses, 
           sum(g.home_score = g.away_score) draws
      from games g
      join team_games tg on tg.gid = g.gid
      join teams t on t.tid = tg.tid
     group by t.tid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many tables in my database which are interrelated. I have a table
I have a one to many relationship between two tables. The many table contains
Many applications have grids that display data from a database table one page at
I have database with many tables. In the first table, I have a field
I have a table called BlogPost which has a 1-to-many relationship with the Comment
I have a many to many index table, and I want to do an
I have a rather large (many gigabytes) table of data in SQL Server that
I have a fixed width DIV containing a table with many columns, and need
I have following situation. A main table and many other tables linked together with
I have a table which is referenced by foreign keys on many other tables.

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.