title: How can i join many SQL tables with a single ID?
body: <p>i have five tables with one links to other tables with ID</p>
  
  <pre><code>TableA &gt; ID | name
  
  TableB &gt; aID | cID
  
  TableC &gt; cID | nameC
  
  TableY &gt; aID | zID
  
  TableC &gt; zID | nameZ
  </code></pre>
  
  <p>connected like this</p>
  
  <pre><code>TableA ID &gt; (TableB aID &gt; TableB cID) &gt; TableC nameC
  TableA ID &gt; (TableB aID &gt; TableY zID) &gt; TableZ nameZ
  </code></pre>
  
  <p>i was using this query for each table when i didn't need to select all of them for each one of them</p>
  
  <pre><code>select A.ID, A.name from TableA A
  left join TableB B on A.ID = B.aID
  inner join TableC C on B.cID = C.cID;
  
  select A.ID, A.name from TableA A
  left join TableY Y on A.ID = Y.aID
  inner join TableC Z on Z.zID = Z.zID;
  </code></pre>
  
  <p>now i join the two of them into a single query to preform a where clause on them both to get the linked results like this</p>
  
  <pre><code>select A.ID, A.name from TableA A
  
  left join TableB B on A.ID = B.aID
  inner join TableC C on B.cID = C.cID
  
  left join TableY Y on A.ID = Y.aID
  inner join TableC Z on Z.zID = Z.zID
  
  where 
  C.nameC = 'nameC1' OR C.nameC = 'nameC2'
  and
  Z.nameZ = 'nameZ1' OR Z.nameZ = 'nameZ3'
  </code></pre>
  
  <p>but in all the queries all results are repeated a many times specially in the last one but i want to get only a single row with A.ID and A.name if it is in the where clause condition and the order them by the one having the highest matches.</p>