join - SQL Many-To-Many within a One-To-Many fetch -
i have table table has one-to-many relation table b. table b has many-to-many relation table c , many-to-many relation table d using join table. have id table want use fetch table b joins. subquery of simple select sufficient?
select tableb.id, tablec.id, tabled.id tableb tableb left join tablec tablec on tableb.id = tablec.id left join tabled tabled on tableb.id = tabled.id tableb.tableaid = :id
is correct?
you join. if want in results then:
select a.*, b.*, c.*, d.* join b on b.id_a = a.id_a left join bc on bc.id_b = b.id_b -- bridge table b <-> c left join c on c.id_c = bc.id_c left join bd on bd.id_b = b.id_b -- bridge table b <-> d left join d on d.id_d = bd.id_b a.id = ?
if don't want in results then:
select b.*, c.*, d.* b left join bc on bc.id_b = b.id_b left join c on c.id_c = bc.id_c left join bd on bd.id_b = b.id_b left join d on d.id_d = bd.id_b b.id_a = ?
if want bs @ least 1 matching c , d, use inner joins instead of outer joins.
Comments
Post a Comment