title: JOIN two tables that share a common grandparent, but not parent
body: <p>I'm trying to unpack a layout system that organizes images and text like so:</p>
  
  <p><img src="https://i.imgur.com/FdN63oH.png" alt="layout"></p>
  
  <p>With the challenge being that I need to (somehow) associate each <code>text.content</code> with both its entry AND the first <code>images.filename</code> that <em>follows</em> it. Essentially introduce second cousins. </p>
  
  <p>Ideally, I'd get something like :</p>
  
  <pre><code>| entry.id  | images.filename  | texts.content
  |-----------|------------------|--------------
  | 0         | img.jpg          | lorem  
  </code></pre>
  
  <p>I've previously been successful at associating the images with their entry using a series of <code>LEFT JOIN</code>s like so:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT entries.id, images.filename 
  FROM images 
  LEFT JOIN containers 
      ON images.container_id = containers.id 
  LEFT JOIN layers 
      ON containers.layer_id = layers.id 
  LEFT JOIN entries 
      ON layers.entry_id = entries.id
  </code></pre>
  
  <p>But this doesn't work when trying to connect the texts to images, because they aren't directly associated.  You have to go up to the entry level to determine association, but how do you then keep track of best-candidate for closest image?</p>
  
  <p>&nbsp;</p>
  
  <h2>Currently</h2>
  
  <p>I feel like I'm closing in on something that will at least associate the images and texts to the same entry with:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT entries.id, images.filename, texts.content
  FROM entries, images, texts
  LEFT JOIN containers a 
      ON texts.container_id = a.id 
  LEFT JOIN containers b
      ON images.container_id = b.id 
  LEFT JOIN layers 
      ON containers.layer_id = layers.id 
  LEFT JOIN entries 
      ON layers.entry_id = entries.id
  </code></pre>
  
  <p>Though this is still kicking back a <code>Not unique table/alias: 'entries'</code> for some reason.</p>
  
  <p>Thusfar I've tried the aliasing stuff suggested <a href="https://stackoverflow.com/questions/8084571/not-unique-table-alias">here</a> (which doesn't really seem to apply)  and began hacking at <a href="https://dba.stackexchange.com/questions/10485/multiple-left-joins-or-subquery">questions dealing with subqueries</a> but ... I've run out of search terms. </p>
  
  <p>&nbsp;</p>
  
  <h2>Is this even possible?</h2>
  
  <p>To be clear, I'm not looking for a whole answer here, just some hints as to how I might use JOINs to do the following:</p>
  
  <p>A) Display texts and images and their entry associations ( I think I'm close here.)</p>
  
  <p>B) <strong>Associate one text entry with data from whatever image entry follows it.</strong>. This feels like an impossibility right now, but I guess that's why I'm asking people who know more than I do. </p>
  
  <p><strong>EDIT : Note that there can be multiple layers containing either images or texts and I need to return <em>all</em> texts from each entry.</strong></p>