sql - JOIN on column only if NOT NULL -


i'm in process of re-writing old sql query , have troubles making sense out of it. contains several conditions of form

select ... a, b, c c.id = ... , (   a.x_id null   or a.x_id = c.x_id ) , b.id = a.b_id (+) 

can query rewritten using proper join syntax? equivalent following or produce different results under circumstances?

select ... b left join on b.id = a.b_id left join c on a.x_id = c.x_id c.id = ... 

the original query 100 lines long , spans 5 tables, plus several joins on "virtual tables" (i.e. conditions of form x.z_id = y.z_id), makes hard break down more manageable bits or debug.

if want same result have in first query - must make left join table a, :

select ...  b, c left join on b.id = a.b_id , b.id = a.b_id c.id = ... b.c_id   

or if want same style tables, can use inner join table b, :

select ...  c inner join b on b.c_id = c.id left join on b.id = a.b_id  c.id = ...  

in both query select data table b column not null


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -