sql - Mysql - Left Join all tables -


i have query looks like:

select 'asdf', '123' ... table1  left join table2 on (     condition1 )  left join table3 on (     condition2 )  (     main_condition ) 

now problem is, need conditionally include table1 well. tried this:

.. .. table1 on (     new_condition ) .. .. 

but wouldn't work. please help.


edit (new finding): in post (http://blog.sqlauthority.com/2010/07/20/sql-server-select-from-dual-dual-equivalent/), found piece of code:

select 1 i, f.bar, f.jar dual left join foo f on f.bar = 1 dual.dummy = ‘x’ union select 2 i, f.bar, f.jar dual left join foo f on f.bar = 2 dual.dummy = ‘x’ 

i'm sure it's not directly related i'm trying do, possible join table dual that?

dummy table:

select record dummy table first. dual such table, built in in mysql exact purpose. wrapped dual in subselect, because mysql apparently doesn't allow left joining against it.

select 'asdf', '123' ...    (select 1 dual) d left join table1 on(   new_condition ) left join table2 on (     condition1 )  left join table3 on (     condition2 ) 

full (outer) join

another solution, though different using full join or full outer join, left join , right join combined. quite different, though can achieve similar result:

select   *   table1   full outer join table2 on joincondition. 

in query above, records both tables returned, if no matching record in either table exists.


Comments