title: Find similar/related data, order by match count
body: <p>My data is spread across multiple tables. Each table looks like:</p>
  
  <pre><code>id | value
  </code></pre>
  
  <p>And there's one master table:</p>
  
  <pre><code>id | type
  </code></pre>
  
  <p>Now I want to select all id's, for a given id, that have at least one value-match in one of the tables, ordered by the number of matches, descending.</p>
  
  <p>Example:</p>
  
  <pre><code>TableMaster (id,type):
  1,typeA
  2,typeA
  3,typeA
  4,typeA
  5,typeB
  
  TableA (id,value):
  1,A
  2,B
  3,B
  4,C
  5,D
  
  TableB (id,value):
  1,Y
  2,X
  3,X
  4,X
  5,X
  
  TableC (id,value):
  1,K
  2,L
  3,L
  4,L
  5,M
  </code></pre>
  
  <p>The wanted sql statement for <strong>id=2</strong> and <strong>type=typeA</strong> returns:</p>
  
  <pre><code>id
  3 (1 match in TableA B=B, 1 match in TableB X=X, 1 match in TableC L=L =&gt; 3 matches)
  4 (1 match in TableB X=X, 1 match in TableC L=L =&gt; 2 matches)
  </code></pre>
  
  <p>id=5 is not returned, because it's typeB.</p>
  
  <p>Is there a efficient way to compare all tables without using a subselect for each table join and how do I count the matches, so I can use the value for ORDER BY?</p>
  
  <p>Thanks!</p>