title: Multi-Level MYSQL Query In One Select Statement
body: <p>Pretty straightforward SQL question here - I have three levels of data spread out over three tables. 
  <strong>Table 1- Listings</strong>, 
  <strong>Table 2 - Interests</strong>,
  <strong>Table 3 - Messages</strong></p>
  
  <p>Table 1 -> Table 2, and Table 2 -> Table 3 have a One-To-Many Relationship</p>
  
  <p><em>There will always be listings, but there may or may not be interests for each listing, and there may or may not be messages for each interest.</em></p>
  
  <p>What I want to do is select all the information in <strong><em>1 Select Statement</em></strong> in order to avoid multiple database calls. </p>
  
  <p>I know with <em>two levels of information</em> you can use GROUP_CONCAT and GROUP BY to grab all the second level information into a concatenated string column. I just don't know what to do with three levels if I want to use only one select.
  I also know how this can be done with two separate calls and a PHP/python script in between, but I want to avoid that for performance. </p>
  
  <p>Ideally I would get back an array that looks like this:</p>
  
  <pre><code>array(
       '1'=&gt;array(listingId'=&gt;1, 
                  interests=&gt;
                             array(
                                   array(
                                         'interestId'=&gt;2
                                         'messages'=&gt;array(
                                                           'messageId'=&gt;5,
                                                           'message'=&gt;'hi'
                                                           ),
                                                     ...
                                         ),
                                  ...
                                  ),
                            ...
                  ),
       ...
       )
  </code></pre>
  
  <p>Another ideal situation would be to get:</p>
  
  <pre><code> array(
       '1'=&gt;array(listingId'=&gt;1, 
                  interests=&gt;
                             array(
                                   array('interestId'=&gt;2,
                                         'messages'=&gt;INSERT CONCATENATED STRING HERE
                                        ),
                                   ...
                                  ),
                             ...
                  ),
       ...
       )
  </code></pre>
  
  <p>With this situation I could just use a PHP/Python/Ruby script to parse the concatenated string of message.</p>
  
  <p><strong>The dots represent more arrays with similar data.</strong></p>
  
  <p>The most significant problem with trying to use concatenation is that you can only pull back one column using a subselect.</p>
  
  <p>Thanks in advance! :)</p>