yin03 yin03 -4 years ago 99
SQL Question

left join and group of inner join

Say, I have the following query:

SELECT * FROM TABLE1
JOIN TABLE2 ON ...
LEFT JOIN TABLE3 ON ...
JOIN TABLE3_1 ON ...
JOIN TABLE3_2 ON ...
JOIN TABLE3_3 ON ...


What I want to achieve is for TABLE3, TABLE3_1, TABLE3_2, TABLE3_3 to have inner joins within them (I only need all the matching data between them, the rest gone). Then for TABLE1, TABLE2 to have inner joins too. But from TABLE1 + TABLE2 result, some won't have a corresponding entries to TABLE3, and that's okay, I will still want it.

Using the above pseudo code if I run it as it is, obviously it will not achieve the same result.

Answer Source

Use paretheses to force joins order, kind of

SELECT * 
FROM (
   TABLE1 
   JOIN TABLE2 ON ...)
LEFT JOIN (
    TABLE3 
    JOIN TABLE3_1 ON ...
    JOIN TABLE3_2 ON ...
    JOIN TABLE3_3 ON ...) ON ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download