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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:29:10+00:00 2026-05-17T01:29:10+00:00

General Case How do you perform a left join across a many-to-many relationship when

  • 0

General Case

How do you perform a left join across a many-to-many relationship when you want to add a condition to the foreign side of the relation?

Specific Case

We’re dealing with two tables: team and pool. There is also a team_pool table serving as a many-to-many junction table between them. Additionally, a pool has a stage_id column.

I want to retrieve all teams with that team’s pool for a specific stage. If the pool doesn’t exist, I want the pool to be NULL. Sample, idealized results:

+--------+----------+------+
| team   | stage_id | pool |
+--------+----------+------+
| Team 1 |        2 | C    |
| Team 2 |     NULL | NULL | //this team doesn't belong to a pool for this stage (but it could belong to a pool for a different stage)
| Team 3 |        2 | G    |
| Team 3 |        2 | H    | //if a team belongs to a 2 pools for the same stage
| Team 4 |        2 | D    |

If it’s relevant, I’m using MySQL.

SQL

Here’s the (simplified) SQL used to create the tables:

CREATE TABLE team (id BIGINT AUTO_INCREMENT, name VARCHAR(50), PRIMARY KEY(id));
CREATE TABLE team_pool (team_id BIGINT, pool_id BIGINT, PRIMARY KEY(team_id, pool_id));
CREATE TABLE pool (id BIGINT AUTO_INCREMENT, stage_id BIGINT, name VARCHAR(40) NOT NULL, PRIMARY KEY(id));

Ideal Solution

The ideal solution would:

  • Not require a change to my schema (ORM really wants it this way)
  • Require a single, non UNION query.

Attempted solutions

  • Use an INNER JOIN rather than a LEFT JOIN from team to team_pool and team_pool to pool. Issue: we lose teams that don’t belong to a pool
  • LEFT JOIN from team to team_pool and team_pool to pool using a WITH condition that the stage_id on pool matches what we’re looking for. Issue: when a team belongs to many pools, we get multiple results. Adding a GROUP BY doesn’t help.

EDIT: Chosen Solution

There are a lot of good solutions here.

Given that my ideal solution is not possible, I’d rather add stage_id to team_pool than use UNION or subqueries. This has the added benefit of letting me enforce that a team can only belong to one pool per stage. It also makes the queries simple:

SELECT t.name, p.name, tp.stage_id FROM team t LEFT JOIN team_pool tp ON t.id = tp.team_id AND tp.stage_id = 2 LEFT JOIN pool p ON p.id = tp.pool_id
  • 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-17T01:29:11+00:00Added an answer on May 17, 2026 at 1:29 am

    If I understand the concepts behind your schema, then I would think stage_id should be a column in team_pool rather than pool. The stage is not an attribute of the pool, it is a factor in the mapping of teams to pools, right?

    Regardless, this is how I would write your query in Oracle. I’m not sure if this exact syntax is right for MySQL. Presumably you would want to parameterize the literal value for stage_id.

    SELECT t.name, p.name
      FROM (SELECT team.name, pool_id
              FROM team LEFT JOIN team_pool
                ON team_pool.team_id = team.team_id ) t
           LEFT JOIN (SELECT pool_id, name FROM pool WHERE stage_id = 2) p
                ON p.pool_id = t.pool_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.