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

  • Home
  • SEARCH
  • 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 4115854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:38:09+00:00 2026-05-20T22:38:09+00:00

I have a three different tables that I’m trying to join up correctly but

  • 0

I have a three different tables that I’m trying to join up correctly but I’m running into some issues.

Here are the tables

  1. Train: Listing of trains (choo-choooo) and train length in number of wagons
  2. WTA: Wagon Tonnes A – Weight of wagons in train at Location A
  3. WTB: Wagon Tonnes B – Weight of wagons in train at Location B

The issue: Some entire Trains are missing randomly from either/or/both of the WagonTonnes tables. Some individual wagons are missing from the the WagonTonnes tables. And I want my query to show those cases as null where missing.

I first tried doing this and it works fine.

select 
 train.id,
 train.length,
 a.position_in_train
 a.tonnes,
from 
 train
 left outer join wta a
 using (train_id)

Now I want to add a left outer join for b, like so

select 
 train.id,
 train.length,
 a.position_in_train
 a.tonnes,
 b.position_in_train,
 b.tonnes
from 
 train
 left outer join wta a
 using (train_id)
 left outer join wtb b
 using (train_id)

But this goes haywire and repeats the same row result over and over.

Proposed Solution

I suspect that I somehow need to get a query from train that instead of looking like so,

train.id train.length
7        163 

looks like this instead

train.id train.position
7        1 
7        2 
7        3
7        4
...      ...
7        162
7        163

And then rewrite my joins to look like this:

left outer join wta a
on (a.train_id = train.train_id and a.position = train.position)

Question 1: Is my general approach to this problem correct? (join-wise)

Question 2: Is my solution correct? If so, how can I implement it?

  • 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-20T22:38:10+00:00Added an answer on May 20, 2026 at 10:38 pm

    Your approach will work, but I think it is easier to LEFT JOIN to a combined dataset representing the superset of WTA and WTB. The two-part UNION ALL emulates FULL JOIN that is lacking in MySQL.

    select 
     t.id,
     t.length,
     wt.a_position_in_train,
     wt.a_tonnes,
     wt.b_position_in_train,
     wt.b_tonnes
    from train t
    left join (
        select a.train_id,
            a.position_in_train a_position_in_train, a.tonnes b_tonnes,
            b.position_in_train b_position_in_train, b.tonnes b_tonnes
        from wta a left join wtb b on a.train_id = b.train_id and a.position_in_train = b.position_in_train
        union all
        select b.train_id, a.position_in_train, a.tonnes, b.position_in_train, b.tonnes
        from wta b left join wtb a on a.train_id = b.train_id and a.position_in_train = b.position_in_train
        where a.train_id is null
    ) wt on t.train_id = wt.train_id
    order by t.train_id, coalesce(a_position_in_train, b_position_in_train)
    

    Brain freeze, for Oracle, the FULL JOIN would be (inner query)

        select coalesce(a.train_id, b.train_id) train_id,
            a.position_in_train a_position_in_train, a.tonnes b_tonnes,
            b.position_in_train b_position_in_train, b.tonnes b_tonnes
        from wta a full join wtb b
            on a.train_id = b.train_id and a.position_in_train = b.position_in_train
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I have three tables that store different types of users with different
I'm completely stumped on this one. I have three different lists that need to
I have three denormalized tables that I have to take at face value (data
I have currently created a facebook like page that pulls notifications from different tables,
I have a database that has different grades per course (i.e. three homeworks for
I have this method, that can return three different response. At first, it was
Alright, I'm trying to link three different tables (Customers, Orders, and Order Details), in
I have a workbook with 20 different pivot tables. Is there any easy way
I have a few problems with my mod_rewrite rules. There a three different url
Anyone have any comparative thoughts on these three technologies? Each addresses a different VM,

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.