title: How to join this data in MySQL?
body: <p>If I have a table like this:</p>
  
  <p><strong><code>TABLEA</code></strong>:</p>
  
  <pre><code>id| object | type
  1 | greg | person
  2 | mary | person
  3 | jared | person
  4 | kelly | person
  5 | melissa | person
  6 | william | person
  7 | skiing | hobby
  8 | biking | hobby
  </code></pre>
  
  <p><strong><code>TABLEB</code></strong>:</p>
  
  <pre><code>id | husband | wife
  1 | greg | mary
  2 | jared | kelly
  3 | william | kelly
  </code></pre>
  
  <p><strong><code>TABLEC</code></strong>:</p>
  
  <pre><code>id | female | hobby
  1 | mary | skiing
  2 | kelly | biking
  </code></pre>
  
  <p>Is there some way I could get a result table of:</p>
  
  <p><strong><code>TABLED</code></strong>:</p>
  
  <pre><code>id | a | b | link
  1 | 1 | 2 | related
  2 | 3 | 4 | related
  3 | 6 | 4 | related
  4 | 1 | 4 | related
  5 | 2 | 7 | likes
  6 | 4 | 8 | likes
  </code></pre>
  
  <p>Using only MySQL query(/ies)? </p>
  
  <ul>
  <li>Logic would basically start iterating from TABLE B and all rows of table B.</li>
  <li>The third column is <code>related</code> when the table being selected is <code>TableB</code>, and is <code>likes</code> when the table being processed is TABLE B.</li>
  </ul>
  
  <p>Sample logic would be:</p>
  
  <p>Looking at the first row of <code>TABLEB</code> (husband) greg and wife (mary), it looks up from TABLEA to see that greg is at row 1 (id 1) and mary is at row 2 (id 2) and creates a new TableD with the first row being 1 | 2. </p>
  
  <p>Is there a query that can do some join or something that would be able to do this without having to programmatically iterate through all rows of TABLEB, then all rows of TABLEC to produce the desired TABLED?</p>