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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:35:14+00:00 2026-06-04T23:35:14+00:00

I’m trying to display some simple computer game results and make it easy to

  • 0

I’m trying to display some simple computer game results and make it easy to iterate through the results line by line in my code. I want it so that all the relevant data for each game is in each record so I can output it all on the one line e.g.:

  • Team A (score 45) vs. Team B (score 55), game duration: 5 mins
  • Team C (score 60) vs. Team D (score 65), game duration: 4.3 mins

So for a game there is two teams that play each other and they each get a score at the end of the game. Essentially there ends up being two rows in the games_teams table for every game.

Here’s my schema:

schema pic

Here’s my table data:

table data pic

Here’s the output I’m trying to achieve so I can easily iterate through the results and output them on the page:

desired output pic

I managed to achieve that with some horrific SQL and lots of subqueries like so:

SELECT games.game_id, game_name, game_duration, 
(SELECT team_id FROM games_teams WHERE games.game_id = games_teams.game_id LIMIT 0, 1) AS team_id_a,
(SELECT team_id FROM games_teams WHERE games.game_id = games_teams.game_id LIMIT 1, 1) AS team_id_b,
(SELECT teams.team_name FROM games_teams INNER JOIN teams ON games_teams.team_id = teams.team_id WHERE games.game_id = game_id LIMIT 0, 1) AS team_name_a,
(SELECT teams.team_name FROM games_teams INNER JOIN teams ON games_teams.team_id = teams.team_id WHERE games.game_id = game_id LIMIT 1, 1) AS team_name_b,
(SELECT team_score FROM games_teams WHERE games.game_id = games_teams.game_id LIMIT 0, 1) AS team_score_a,
(SELECT team_score FROM games_teams WHERE games.game_id = games_teams.game_id LIMIT 1, 1) AS team_score_b
FROM games

Problem with that method is it’ll be slow and it doesn’t scale. I also need to pull out other game stats from the games_teams table so that’ll be even more subqueries.

The other method I tried was:

not desired output pic

I achieved that with the following SQL:

SELECT games.game_id, game_name, game_duration, teams.team_id, team_name, team_score
FROM games
INNER JOIN games_teams ON games.game_id = games_teams.game_id
INNER JOIN teams ON games_teams.team_id = teams.team_id

Now this way will be harder to foreach through in the code as the relevant data for each game is in two different records. I’d have to build the first part of the row, then go into the next loop iteration and print the next part. Then start it all over again for the next game, I’m trying to display all the information on one line like:

Team A (score 45) vs Team B (score 55), game duration: 5mins

So that’s why I think it would be easier if it was all on the one record. Is there a way to accomplish this nicely and so it scales as well if I need more columns in the games_teams table?

Here’s a pastebin link with the database code if you need to recreate it.

Any help much appreciated, thanks!

  • 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-04T23:35:15+00:00Added an answer on June 4, 2026 at 11:35 pm

    You’ll need to join the games_teams and teams twice, like:

    SELECT ga.game_id
            , ga.game_name
            , ga.game_duration
            , t1.team_name, gt1.team_score
            , t2.team_name, gt2.team_score
    FROM games ga
    JOIN games_teams  gt1 ON gt1.game_id = ga.game_id
    JOIN games_teams  gt2 ON gt2.game_id = ga.game_id
    JOIN teams t1 ON t1.team_id = gt1.team_id
    JOIN teams t2 ON t2.team_id = gt2.team_id
    WHERE gt1.team_id < gt2.team_id
            ;
    

    A clean way to do squeeze out the {games_teams * teams} sub-join and refer to it twice is by putting it into a CTE: (unfortunately mysql does not support CTEs)

    WITH gtx AS (
            SELECT gt.game_id
            , gt.team_score
            , te.team_id
            , te.team_name
            FROM games_teams gt
            JOIN teams te ON te.team_id = gt.team_id
            )
    SELECT ga.game_id 
            , ga.game_name
            , ga.game_duration
            , g1.team_name, g1.team_score
            , g2.team_name, g2.team_score
    FROM games ga
    JOIN gtx g1 ON g1.game_id = ga.game_id
    JOIN gtx g2 ON g2.game_id = ga.game_id
    WHERE g1.team_id < g2.team_id
      ;
    

    Result:

     game_id | game_name | game_duration | team_name | team_score | team_name | team_score 
    ---------+-----------+---------------+-----------+------------+-----------+------------
           1 | Game A    |           300 | Team A    |         45 | Team B    |         55
           2 | Game B    |           258 | Team C    |         60 | Team D    |         65
    (2 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
i want to parse a xhtml file and display in UITableView. what is the
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.