MySQL nested left join with external links

Say we have these (simplified from more complex) table examples:

== st ==    == pr ===   == rn ===  <– tables
sta   pg    pg    rou   sta   rou  <– fields
========    =========   =========
H1    aa    aa    aaA   H1    aaA

H2    aa    aa    aaB   H2    aaB
H3    aa                H3    aaB

H4    aa    aa    aaC   H4    aaC
H5    aa                H5    aaC
H6    aa                H6    aaC

H7    aa

H8    bb    bb    NULL

I wanted to execute this (also simplified) query with an inner left join:

SELECT st.*, pr.*, rn.*
FROM         st
INNER JOIN ( pr
LEFT  JOIN   rn  ON pr.rou = rn.rou
                AND          rn.sta = st.sta -- ERROR here
           )     ON pr.pg =           st.pg
-- filter out bad rows
WHERE ( rn.id )                          -- a: not null
   OR ( pr.rou ='aaC' AND rn.id IS NULL) -- b: no joinable rn found: choose
                                         --    by a predefined pr.rou value
   OR ( pr.rou IS NULL )                 -- c: no need to join

to get this result:

== st ==    == pr ===   == rn ===
sta   pg    pg    rou   sta   rou
========    =========   =========
H1    aa    aa    aaA   H1    aaA
H2    aa    aa    aaB   H2    aaB
H3    aa    aa    aaB   H3    aaB
H4    aa    aa    aaC   H4    aaC
H5    aa    aa    aaC   H5    aaC
H6    aa    aa    aaC   H6    aaC

H7    aa    aa    aaA   NULL  NULL \  H7 has no rn, so choose
H7    aa    aa    aaB   NULL  NULL  } 1 row of these at the
H7    aa    aa    aaC   NULL  NULL /  WHERE / b condition

H8    bb    bb    NULL  NULL  NULL

but MySQL is causing this error: #1054 - Unknown column 'st.sta' in 'on clause'.

I tried to solve this problem without success until someone submitted (and deleted) a good idea to join the column rntwice. Thanks to him / her, I was able to create a working solution:

SELECT st.*, pr.*, rn.*, rn2.*

FROM         st
INNER JOIN   pr         ON st.pg = pr.pg
LEFT  JOIN   rn         ON st.sta = rn.sta
LEFT  JOIN   rn AS rn2  ON pr.rou = rn2.rou -- the two rn join order is important
                       AND rn.id  = rn2.id  -- if first exists, second must match or null, first null => second null
WHERE ( rn.id = rn2.id )                    -- a: both not null
   OR ( pr.rou ='aaC' AND rn.id IS NULL)    -- b: no joinable rn found: choose by predefined pr.rou value
   OR ( pr.rou IS NULL )                    -- c: no need to join

As long as this query works, it requires duplicate joins in the correct order, very fragile and ugly.

Can you give a cleaner solution that joins a table rnonly once?

Here is an example database in sql for copypasting if you want to play with it:

DROP    TABLE IF     EXISTS st;
CREATE  TABLE IF NOT EXISTS st (
  id  int  AUTO_INCREMENT,
  sta varchar(9),
  pg  varchar(9),
  PRIMARY KEY ( id )
)  AUTO_INCREMENT=1;

DROP    TABLE IF     EXISTS pr;
CREATE  TABLE IF NOT EXISTS pr (
  id  int  AUTO_INCREMENT,
  pg  varchar(9),
  rou varchar(9),
  PRIMARY KEY ( id )
)  AUTO_INCREMENT=1;

DROP    TABLE IF     EXISTS rn;
CREATE  TABLE IF NOT EXISTS rn (
  id  int  AUTO_INCREMENT,
  sta varchar(9),
  rou varchar(9),
  PRIMARY KEY ( id )
)  AUTO_INCREMENT=1;


INSERT INTO st 
(sta , pg ) VALUES
('H1','aa'),
('H2','aa'),
('H3','aa'),
('H4','aa'),
('H5','aa'),
('H6','aa'),
('H7','aa'),
('H8','bb');
INSERT INTO pr
( pg , rou ) VALUES
('aa','aaA'),
('aa','aaB'),
('aa','aaC'),
('bb', NULL);
INSERT INTO rn
(sta , rou ) VALUES
('H1','aaA'),
('H2','aaB'),
('H3','aaB'),
('H4','aaC'),
('H5','aaC'),
('H6','aaC');
+5
5

, , , :

SELECT st.*, pr.*, rn.*
FROM st
  LEFT JOIN rn ON st.sta = rn.sta
  LEFT JOIN pr ON st.pg  = pr.pg
              AND (rn.rou = pr.rou OR rn.rou IS NULL)
+11
select st.*, pr.*, rn.*  
from st
left join  pr on pr.pg = st.pg
left  join   rn  on rn.sta = st.sta
+2

I experienced it quickly and seemed to work. I renamed the columns to show the context of what they should represent in order to match the expected results.

SELECT 
      st.sta,
      st.pg,
      MIN( COALESCE( prByRN.pg, pr.pg )) prpg,
      MIN( COALESCE( prByRN.rou, pr.rou )) prrou,
      MIN( rn.sta ) rnsta,
      MIN( rn.rou ) rnrou
   from
      st
         left join rn
            ON st.sta = rn.sta 
            LEFT JOIN pr prByrn
               ON rn.rou = prByrn.rou
         LEFT JOIN pr
            ON st.pg = pr.pg
   GROUP BY 
      st.sta,
      st.pg
+1
source

This request is 100% right ...

Check this.....

Select st.sta,st.pg,pr.pg,pr.rou,rn.sta,rn.rou from st
    Left join pr 
                     On pr.pg=st.pg
    Left join rn 
                     On rn.sta=st.sta

Your result was given this query ...

If any doubt then answer as soon as possible.

+1
source
SELECT st.*, pr.*, rn.*
FROM 
    st
  LEFT  JOIN 
      ( pr                          --- the parenthesis are
      LEFT  JOIN 
        rn 
          ON  pr.rou = rn.rou
      )                             --- there for clarity
    ON  rn.sta = st.sta 
    AND pr.pg =  st.pg
0
source

All Articles