I, also, have a question regarding how to join my database tables. My data setup is as follows:
MainTable
main_id x y z
1       l m n
2       o p q
SubTableA
main_id extra_id foo bar
1       a        1   4
1       b        2   h
1       c        1   g
2       a        k   er
2       b        k   34
2       c        l   f
SubTableB
main_id extra_id a b c
1       a        v r j
1       c        w r k
2       c        x h l
SubTableC
main_id extra_id mono stereo
2       c        n    null
2       d        z    y
I'm trying to achieve the following result:
main_id x y z extra_id foo bar a b c mono stereo
1       l m n a        1   4   v r j null null
1       l m n b        2   h         null null
1       l m n c        1   g   w r k null null
2       o p q a        k   er        null null
2       o p q b        k   34        null null
2       o p q c        l   f   x h l n    null
2       o p q d                      z    y
To spice things up, sometimes the extra_id is null or one of the subtables doesn't contain main_id but then I would like the other tables to be joined and null-filled for values that aren't found.
In that case I would like the outcome to be:
main_id x y z extra_id foo bar a b c null null for example.
I tried to use How can I join multiple SQL tables using the IDs? as template but couldn't get it all the way.
This is what I tried so far:
SELECT
  m.*,
  a.*,
  b.*,
  c.*,
FROM
  MainTable m,
  SubTableA a,
  SubTableB b,
  SubTableC c,
WHERE
  m.main_id = a.main_id(+)
  AND a.main_id=b.main_id(+)
  AND a.extra_id=b.extra_id(+)
  AND a.main_id = c.main_id(+)
  AND a.extra_id = c.extra_id(+);
Edit:
I want all rows from main table. When they match sub tables on main_id join them for each of the extra_id (which can be more than one for each main_id). And if combination of main_id and extra_id exists in more than one sub table I want them to be joined. Will give more elaborated example when in front of laptop again.
Thanks,
Christian