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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:09:46+00:00 2026-05-15T10:09:46+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. SHOW CREATE TABLE information follows.

I have been messing with this query for a while. At this point I don’t care if it requires sub-queries, joins, or unions. I have tried all of them but nothing has been satisfactory and I think i’m missing something. The disconnect I keep having is finding the two team ids from each game and then using the tid to grab the team information.

What I would like to do is if given a game id (gid) I can query to find:

home_team_name, home_team_id, away_team_name, away_team_id, team_league(away and home will be the same league), all the information from games table


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-15T10:09:47+00:00Added an answer on May 15, 2026 at 10:09 am

    I assumed homeoraway = 1 for home and homeoraway = 0 for away.

    SELECT g.*, ht.name, ht.tid, at.name, at.tid, ht.league
    FROM games g
    JOIN team_games htg ON htg.gid = g.gid AND htg.homeoraway = 1
    JOIN team ht ON ht.tid = htg.tid
    JOIN team_games atg ON atg.gid = g.gid AND atg.homeoraway = 0
    JOIN team at ON at.tid = atg.tid
    

    This works by joining games to team_games for the home team, then to teams for the team info, then doing the same thing for the away team.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many to many table setup in my mysql database. Teams can
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 table setup like this (simplified for example): user_id item_id click_dt Each
I have a database setup with many tables. To Find a User's Computer it
First, the setup: I have a table in an Oracle10g database with spatial columns.
I have a many to many index table, and I want to do an

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.