title: Selecting nested data from multiple MYSQL tables
body: <p>i have to select nested data from multiple tables.</p>
  
  <p>My tables are  betting_sport, betting_groups, betting_leagues, and betting_matches.</p>
  
  <p>Every sport has many groups, groups have many leagues and leagues have many matches.</p>
  
  <p><img src="https://i.stack.imgur.com/J2YU7.png" alt="This is screenshot for my tables"></p>
  
  <p>I want to select nested tree for <strong>sports->groups->leagues</strong> which have only matches that have status=1</p>
  
  <p>I tried this select, but it doesn't work:</p>
  
  <pre><code>            select
                  betting_sport.id as s_id,
                  betting_sport.title as s_title,
                  betting_group.id as g_id,
                  betting_group.title as g_title,
                  betting_league.id as l_id,
                  betting_league.title as l_title
              from
                  betting_sport
              left join
                  betting_group
              on
                  betting_group.betting_sport_id = betting_sport.id
              left join
                  betting_league
              on
                  betting_league.betting_group_id = betting_group.id
              WHERE
                  betting_sport.id
              IN
              (
                  select betting_sport_id from group where id in (
                      select
                          betting_group_id
                      from
                          betting_league
                      where
                          betting_league.id in (
                           select betting_league_id from betting_match where status=1
                          )
                  )
              )
  </code></pre>