Hi,

I have a grandparent, parent and child relationship. Each is a business_id. each business may (or may not), have a stylesheet record in the second table.

I will be entering an id to the WHERE clause. I need to bring back those ids on senior levels, which do have a record in table_2.

so, if a child_id is say 32 and its parent records each has a record in table_2 (AND the live_or_dev col is 'live'), I need to get those ids, in order from grandparent->parent->child - or even grandparent->child OR grandparent->parent. Just whatever ids have a stylesheet

I can't get my head around it but it seems to be join issue but I am not sure.

Code MySQL:
SELECT
       level_3.id as id_3
     , level_2.id as id_2
     , level_1.id as id_1
  from businesses AS level_1			            
left outer
  JOIN businesses as level_2
    on level_2.id = level_1.parent_id
left outer
  join stylesheets as L2_s
    on L2_s.business_id = level_2.id
   and L2_s.live_or_dev = 'live'
left outer
  JOIN businesses as level_3
    on level_3.id = level_2.parent_id
left outer
  join stylesheets as L3_s
     on L3_s.business_id = level_3.id
   and L3_s.live_or_dev = 'live'
left outer
  join stylesheets as L1_s
    on L1_s.business_id = level_1.id
   and L1_s.live_or_dev = 'live'
 WHERE level_1.id = 7